APIet støtter OData batch requests som beskrevet på odata.org. Dette er nyttig i tilfeller der det er behov for data som er koblet sammen, som for eksempel en sjekklistebesvarelse.

En batch request er gjennomført via en multipart/mixed spørring. Hver del er et HTTP dokument, som igjen er en vanlig spørring. Dette er gjort slik fordi en multipart del har headers, men ikke verb og url (POST /api/v20...).

Dersom man ønsker å referere til resultatet fra en tidligere request, må man oppgi Content-ID  header i requesten som skal refereres til. For eksempel en oppretting. URL/Id til objektet som påvirkes av spørringen lagres i variabel, som siden kan brukes. For eksempel kan man oppgi Content-ID: 1  i en POST /odata/v24/SurveyResult  spørring, og senere i en oppretting av SurveyAnsver kan man oppgi "ResultId": "$1" . Merk at selv om ResultId vanligvis er et nummer, må det være $1 som string for å oppfylle JSON-standarden.

Eksempel

POST /odata/v24/$batch HTTP/1.1
Host: company.landax.no
Content-Type: multipart/mixed;boundary=batch_cb2ce436-ea4e-4e8a-8a63-171ea74a0146
OData-Version: 4.0
Authorization: Bearer TOKEN
Content-Length: ###

--batch_cb2ce436-ea4e-4e8a-8a63-171ea74a0146
Content-Type: multipart/mixed;boundary=changeset_cdaf34bc-2f49-4613-b32e-3e3773c371bc

--changeset_cdaf34bc-2f49-4613-b32e-3e3773c371bc
Content-Type: application/http
Content-Transfer-Encoding: binary
Content-ID: 1

POST /odata/v24/SurveyResult HTTP/1.1
Host: company.landax.no
Content-Type: application/json
Content-Length: ###

{"DefinitionId": 287, "Name": "Test Survey Result"}
--changeset_cdaf34bc-2f49-4613-b32e-3e3773c371bc
Content-Type: application/http
Content-Transfer-Encoding: binary
Content-ID: 2

POST /odata/v24/SurveyAnswer HTTP/1.1
Host: company.landax.no
Content-Type: application/json
Content-Length: ###

{"ResultId":"$1","QuestionId": 3669, "Text": "Answer Text"}
--changeset_cdaf34bc-2f49-4613-b32e-3e3773c371bc
Content-Type: application/http
Content-Transfer-Encoding: binary
Content-ID: 3

POST /odata/v24/SurveyAnswer HTTP/1.1
Host: company.landax.no
Content-Type: application/json
Content-Length: ###

{"ResultId":"$1","QuestionId": 3670, "Value": 123}
--changeset_cdaf34bc-2f49-4613-b32e-3e3773c371bc
Content-Type: application/http
Content-Transfer-Encoding: binary
Content-ID: 4

POST /odata/v24/Document/CreateDocument HTTP/1.1
Host: company.landax.no
Content-Type: multipart/form-data;boundary=data_cb2ce436-ea4e-4e8a-8b61-171ea74a12145
Content-Length: ###

--data_cb2ce436-ea4e-4e8a-8b61-171ea74a12145
Content-Disposition: form-data; name="document"

{ "FileName": "image.jpg", "ModuleId": 120 }
--data_cb2ce436-ea4e-4e8a-8b61-171ea74a12145
Content-Disposition: form-data; name="documentLink"

{ "SurveyAnswerId": "$2" }
--data_cb2ce436-ea4e-4e8a-8b61-171ea74a12145
Content-Disposition: form-data; name="fileData"; filename="image.jpg"
Content-Type: image/jpeg

##BINARY FILE DATA##
--data_cb2ce436-ea4e-4e8a-8b61-171ea74a12145--
--changeset_cdaf34bc-2f49-4613-b32e-3e3773c371bc--
--batch_cb2ce436-ea4e-4e8a-8a63-171ea74a0146--

Will result in this response:

HTTP/1.1 200 OK
Content-Type: multipart/mixed;boundary=b_33d00764-8fe7-5bba-83fe-bf2fb35fe046
OData-Version: 4.0
DataServiceVersion: 4.0
--headers--
Content-Length: ###

--b_33d00764-8fe7-5bba-83fe-bf2fb35fe046
Content-Type: multipart/mixed;boundary=cs_d0463014-5092-56b1-975f-fe66b9e1d409

--cs_d0463014-5092-56b1-975f-fe66b9e1d409
Content-Type: application/http
Content-Transfer-Encoding: binary
Content-ID: 1

HTTP/1.1 201 Created
Content-Type: application/json;charset=utf-8
Location: https://company.landax.no/odata/v24/SurveyResults(2913)
--headers--
Content-Length: ###

{"SurveyId":171,"DefinitionId":287,"Number":"4", ...result data..., "@odata.context":"/api/v20/$metadata#SurveyResults/$entity","@odata.type":"#Landax.Data.SurveyResult"}
--cs_d0463014-5092-56b1-975f-fe66b9e1d409
Content-Type: application/http
Content-Transfer-Encoding: binary
Content-ID: 2

HTTP/1.1 201 Created
Content-Type: application/json;charset=utf-8
--headers--
Location: https://company.landax.no/odata/v24/SurveyAnswers(39089)
Content-Length: ###

{"ResultId":2913,"QuestionId":3669,...answer data...,"@odata.context":"/api/v20/$metadata#SurveyAnswers/$entity","@odata.type":"#Landax.Data.SurveyAnswer"}
--cs_d0463014-5092-56b1-975f-fe66b9e1d409
Content-Type: application/http
Content-Transfer-Encoding: binary
Content-ID: 3

HTTP/1.1 201 Created
Content-Type: application/json;charset=utf-8
...headers...
Location: https://company.landax.no/odata/v24/SurveyAnswers(39090)
Content-Length: ###

{"ResultId":2913,"QuestionId":3670,...answer data...,"@odata.context":"/api/v20/$metadata#SurveyAnswers/$entity","@odata.type":"#Landax.Data.SurveyAnswer"}
--cs_d0463014-5092-56b1-975f-fe66b9e1d409
Content-Type: application/http
Content-Transfer-Encoding: binary
Content-ID: 4

HTTP/1.1 200 OK
Content-Type: application/json;charset=utf-8
...headers...
Content-Length: ###

{"@odata.context":"/odata/v24/$metadata#Document/CreateDocument","value":{"document":{"Id":10985,"DocumentId":10985,"VersionId":10916,"Status":"published","FileName":"image.jpg", ...document data...},"documentLink":{"Id":9445,"Guid":"757c11f9-46a5-5e75-9da2-fdae48967c7c","DocumentId":10985,"VersionId":10916,"SurveyAnswerId":39089,...link data...}}}
--cs_d0463014-5092-56b1-975f-fe66b9e1d409--
--b_33d00764-8fe7-5bba-83fe-bf2fb35fe046--

The response parts will keep the same "Content-ID" as the request parts, so it is easy to map them. HTTP parsing must be done by client. This is a typical http multipart response, and there should be parsers out there for those.

Manual parsing can be done by splitting on first double empty line. Above those are headers, below those until --boundary will be content. Content can be parsed through same function until we have a plain body.


  • No labels