ws-deprecated icon indicating copy to clipboard operation
ws-deprecated copied to clipboard

How send request get with params ?

Open ngocdtph03070 opened this issue 9 years ago • 15 comments

I have a request get with format http://192.168.1.4:1337/api/classes/Place?where={"category":{"$inQuery":{"where":{"objectId":"GPZPzdsk0I"},"className":"Category"}},"coordinate":{"$nearSphere":{"__type":"GeoPoint","latitude":20.286312208425457,"longitude":106.30682822629947}, "$maxDistanceInKilometers": 15.0 }}&include=category,user and test api on post man is ok as I convert to params in swift is can not get.I think url encoding not right. You can help me ?

ngocdtph03070 avatar Oct 17 '16 06:10 ngocdtph03070

Hey @ngocdtph03070,

Could you provide the part of the code where you build the request so I can check it out ?

Cheers,

s4cha avatar Oct 17 '16 07:10 s4cha

@s4cha Yes , Thank for reply This code let nearSphere = ["$nearSphere": ["__type":"GeoPoint","latitude":20.286312208425457,"longitude": 106.30682822629947], "$maxDistanceInKilometers": 1.0 ] as [String : Any]

var parameters = ["where":["coordinate":nearSphere],"include":"category,user"] return API.get("classes/Place",params:parameters)

ngocdtph03070 avatar Oct 17 '16 07:10 ngocdtph03070

the param string generated by ws is ?include=category%2Cuser&where%5Bcoordinate%5D%5B%24maxDistanceInKilometers%5D=1&where%5Bcoordinate%5D%5B%24nearSphere%5D%5B__type%5D=GeoPoint&where%5Bcoordinate%5D%5B%24nearSphere%5D%5Blatitude%5D=20.28631220842546&where%5Bcoordinate%5D%5B%24nearSphere%5D%5Blongitude%5D=106.3068282262995

which is ?include=category,user&where[coordinate][$maxDistanceInKilometers]=1&where[coordinate][$nearSphere][__type]=GeoPoint&where[coordinate][$nearSphere][latitude]=20.28631220842546&where[coordinate][$nearSphere][longitude]=106.3068282262995

whereas the one you pasted above generates

?where={%22category%22:{%22$inQuery%22:{%22where%22:{%22objectId%22:%22GPZPzdsk0I%22},%22className%22:%22Category%22}},%22coordinate%22:{%22$nearSphere%22:{%22__type%22:%22GeoPoint%22,%22latitude%22:20.286312208425457,%22longitude%22:106.30682822629947},%20%22$maxDistanceInKilometers%22:%2015.0%20}}&include=category,user

which should be ?where={"category":{"$inQuery":{"where":{"objectId":"GPZPzdsk0I"},"className":"Category"}},"coordinate":{"$nearSphere":{"__type":"GeoPoint","latitude":20.286312208425457,"longitude":106.30682822629947}, "$maxDistanceInKilometers": 15.0 }}&include=category,user

This is indeed an encoding issue.

The dictionary is not automatically translated into JSON, which is normal, I guess.

Something like this would produce the desired output



func test() -> Promise<JSON> {
    let categoryJson = "{\"category\":{\"$inQuery\":{\"where\":{\"objectId\":\"GPZPzdsk0I\"},\"className\":\"Category\"}},\"coordinate\":{\"$nearSphere\":{\"__type\":\"GeoPoint\",\"latitude\":20.286312208425457,\"longitude\":106.30682822629947}, \"$maxDistanceInKilometers\": 15.0 }}"
    return ws.get("/classes/Place", params:[
        "where": categoryJson,
        "include":"category,user"])
}

There is no helper method on our side at the moment to transform a [String:Any] into a JSON string, because we never had the case. This surely looks like something we'd like to add to our roadmap though :)

Cheers,

s4cha avatar Oct 17 '16 08:10 s4cha

@s4cha Thank you I try code for format and talk for you :)

ngocdtph03070 avatar Oct 17 '16 08:10 ngocdtph03070

@ngocdtph03070 What technology are you using on the backend side ? I am curious :)

s4cha avatar Oct 17 '16 08:10 s4cha

@s4cha i use parse server platform but i want rest api not use sdk

ngocdtph03070 avatar Oct 17 '16 08:10 ngocdtph03070

Ahh cool! thanks for the info :)

s4cha avatar Oct 17 '16 08:10 s4cha

@s4cha thank you for framework it is good .

ngocdtph03070 avatar Oct 17 '16 08:10 ngocdtph03070

@ngocdtph03070 Does your call work now ?

s4cha avatar Oct 17 '16 12:10 s4cha

Yes it run :)

ngocdtph03070 avatar Oct 17 '16 12:10 ngocdtph03070

@ngocdtph03070 I guess you already have a helper but here is a way to avoid having to hardcode and escape the JSON string ourselves.

func dictionaryToJSONString(_ d:[String : Any]) -> String {
    if let data = try? JSONSerialization.data(withJSONObject: d), let s = String(data: data, encoding: String.Encoding.utf8) {
        return  s
    } else {
        return ""
    }
}

For a given JSON structure

{
    "category": {
        "$inQuery": {
            "where": {
                "objectId": "GPZPzdsk0I"
            },
            "className": "Category"
        }
    },
    "coordinate": {
        "$nearSphere": {
            "__type": "GeoPoint",
            "latitude": 20.286312208425457,
            "longitude": 106.30682822629947
        },
        "$maxDistanceInKilometers": 15.0
    }
}

Write it as a native dictionary and use helper method to get back the JSON string

func test() -> Promise<Void> {
    let dic:[String : Any] = [
        "category": [
            "$inQuery": [
                "where": [
                    "objectId" : "GPZPzdsk0I"
                ],
                "className" : "Category"
            ]
        ],
        "coordinate" : [
            "$nearSphere": [
                "__type": "GeoPoint",
                "latitude": 20.286312208425457,
                "longitude": 106.30682822629947
            ],
            "$maxDistanceInKilometers": 15.0
        ]
    ]
    let jsonString = dictionaryToJSONString(dic)
    return ws.get("/api/classes/Place",
                  params:["where" : jsonString,
                          "include": "category,user"])
}

Let me know what you think,

Have an awesome day !

s4cha avatar Oct 26 '16 11:10 s4cha

Yes it good instead of fix string json func convert dictionnary to string json Thank you

ngocdtph03070 avatar Oct 26 '16 11:10 ngocdtph03070

I think ws framework should support http request param body

ngocdtph03070 avatar Oct 26 '16 11:10 ngocdtph03070

We are very open to suggestions, could you elaborate on how the api would look like ?

s4cha avatar Oct 26 '16 14:10 s4cha

As Parse server when i rest api in case post data success with httpBody raw format json . opposite i rest api send params receive status code success but data param not insert to database @s4cha

ngocdtph03070 avatar Oct 26 '16 14:10 ngocdtph03070