I have constructed this JSON using a fair amount of string interpolation:
{
"headers":{
"email":"email@example.org",
"rank":0,
"blue":false,
"team":1000,
"round":33,
"tournament_id":"7F98sdh98aFH98h"
},
"data":{
"start_position":0.0,
"crossed_line":true,
"end_platform":true,
"lift":0,
"first-actions":[
{
"timestamp":1520403299.17746,
"action":"0_0_1"
}
],
"second-actions":[
{
"timestamp":1520403299.96991,
"action":"0_0_2"
}
]
}
}
I tried to include this in my POST request's httpBody
like so:
request.httpBody = json.data(using: .utf8)
However, this results in a 422 error.
Serverside, the result is all of the string being interpreted as a single header:
--- NEW REQUEST ---
Time: March 6th 2018, 8:47:23 pm (1520398043327)
IP: [REDACTED]
Request: [REDACTED]
req.body = {'{"headers":{"email":"email@example.org","rank":0,"blue":false,"team":1000,"round":20,"tournament_id":"7F98sdh98aFH98h",},"data":{"start_position":-36.5385,"crossed_line":true,"end_platform":true,"lift":0,"first-actions":[{"timestamp":1520398021.45196,"action":"0_0_1"}],"second-actions":[{"timestamp":1520398022.73314,"action":"0_0_2"}]}}':''}
Auth: [REDACTED]
Auth level: 10
I then realised that it should be sent as a JSON Object, not a string. I have tried many ways of doing this, including converting json
to a Dictionary, but then converting that to Data gives a runtime error.
How should I go about converting the string to the correct format?
EDIT: Result from Dávid's answer:
--- NEW REQUEST: 60 ---
[REQ 60] Time: March 7th 2018, 8:52:39 pm (1520484759369)
[REQ 60] IP: [REDACTED]
[REQ 60] Request: [REDACTED]
[REQ 60] req.body = { '{"headers":{"team":"1000","email":"email@example.org","rank":"0","blue":"false","round":"22","tournament_id":"7F98sdh98aFH98h"},"data":{"lift":"0","crossed_line":"true","end_platform":"true","first-actions":[{"timestamp":0,"action":"0_0_0"},{"timestamp":1520484747.061681,"action":"0_0_1"}],"second-actions":[{"timestamp":0,"action":"0_0_0"},{"timestamp":1520484747.9255838,"action":"0_0_2"}],"start_position":"0.0"}}': '' }
Auth: [REDACTED]