typicode/jsonplaceholder

userId is Int when GET, but String when get result after POST

elijah-rappaport opened this issue · 0 comments

There is a discrepancy in the type for userId. This appears to be an issue on all the endpoints. Below is an illustration of the issue on the "posts" endpoint.

When I GET a post, userId is an Int, as you see here...

GET 'https://jsonplaceholder.typicode.com/posts/1'
curl "https://jsonplaceholder.typicode.com/posts/1"
200 'https://jsonplaceholder.typicode.com/posts/1'
{
"userId": 1, <==== Int
"id": 1,
"title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit",
"body": "quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto"
}

When I get the result after POST a new post, userId is a String, as you see here...

POST 'https://jsonplaceholder.typicode.com/posts'
Content-Type : application/x-www-form-urlencoded
HttpBody : body=B&title=A&userId=1&id=-1
curl "https://jsonplaceholder.typicode.com/posts"
-X POST
-H 'Content-Type: application/x-www-form-urlencoded'
-d 'body=B&title=A&userId=1&id=-1'
201 'https://jsonplaceholder.typicode.com/posts'
{
"body": "B",
"title": "A",
"userId": "1", <==== String
"id": 101
}

This issue causes errors in my code when trying to decode or encode using the same struct
struct Post: Codable {
let id: Int?
let userId: Int <== this will cause error when trying to convert the json received back from a POST, as I'm expecting an Int but getting a String
var title: String
var body: String
}