freshOS / ws-deprecated

⚠️ Deprecated - (in favour of Networking) :cloud: Elegantly connect to a JSON api. (Alamofire + Promises + JSON Parsing)
MIT License
353 stars 32 forks source link

POST params as JSON? #6

Closed stami closed 8 years ago

stami commented 8 years ago

How can I serialize POST params as JSON?

Let's say we have save() like this

func save() -> Promise<MyModel> {
    return ws.post("/mymodels", params: ["data": ["date": startingDate, "number": 123]])
}

Log output is

POST /mymodels
params : ["data": {
    date = "2016-04-21 18:26:53 +0000";
    number = 123;
}]
CODE: 400

So params should be something like

{
  "data": {
    "date" = "2016-04-21 18:26:53 +0000",
    "number" = 123
  }
}

Can this be accomplished easily with ws, or should I be looking for alternatives or plain Alamofire?

s4cha commented 8 years ago

@stami Thanks a lot for your question. This is not supported at the moment, solely because we didn't have the need for it internally. I recognise it is a super common scenario for many apis so we need so support this asap. If you're in a hurry I'd say do it with pure Alamofire, but we are going to integrate this shortly (~1 week). Thanks for helping us improve ws :)

s4cha commented 8 years ago

@stami we landed a first implementation on branch "multipart". You can now configure post parameter encoding like so :

ws.postParameterEncoding = .JSON

Default is .URL

If you could try it out that'd be awesome \o/

stami commented 8 years ago

Nice, thanks! I'll try it out and let you know about any issues I encounter.

stami commented 8 years ago

So far, so good. I had other issues too in my first comment (that HTTP 400 was about wrong url and that log print wasn't the actual json string that was sent). I got the url right and then it was just about parameter encoding.

I'm sending objects like below and everything is working. 😊 (https://github.com/stami/Location/blob/master/Location/Exercise.swift)

{
  "startingDate": "2016-04-25T15:29:03+0300",
  "averageSpeed": 33.27111135904285,
  "totalDistance": 66.65936915586187,
  "description": "Nice running",
  "trace": [
    {
      "latitude": 37.47004943,
      "longitude": -122.29347317,
      "timestamp": "2016-04-25T15:29:03+0300"
    },
    ...
  ]
}
stami commented 8 years ago

I needed JSON parameter encoding also with PUT method and made a pull request about it. If you merge the request (or do something better) I consider this issue closed. 😊

s4cha commented 8 years ago

@stami version 1.2 just got released and it packs the additions we made on multipart :) Thanks a ton for your help on this feature 👍