Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How send request get with params ? #22

Open
ngocdtph03070 opened this issue Oct 17, 2016 · 15 comments
Open

How send request get with params ? #22

ngocdtph03070 opened this issue Oct 17, 2016 · 15 comments

Comments

@ngocdtph03070
Copy link

ngocdtph03070 commented Oct 17, 2016

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 ?

@s4cha
Copy link
Member

s4cha commented Oct 17, 2016

Hey @ngocdtph03070,

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

Cheers,

@ngocdtph03070
Copy link
Author

ngocdtph03070 commented Oct 17, 2016

@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)

@s4cha
Copy link
Member

s4cha commented Oct 17, 2016

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,

@ngocdtph03070
Copy link
Author

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

@s4cha
Copy link
Member

s4cha commented Oct 17, 2016

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

@ngocdtph03070
Copy link
Author

ngocdtph03070 commented Oct 17, 2016

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

@s4cha
Copy link
Member

s4cha commented Oct 17, 2016

Ahh cool! thanks for the info :)

@ngocdtph03070
Copy link
Author

@s4cha thank you for framework it is good .

@s4cha
Copy link
Member

s4cha commented Oct 17, 2016

@ngocdtph03070 Does your call work now ?

@ngocdtph03070
Copy link
Author

Yes it run :)

@s4cha
Copy link
Member

s4cha commented Oct 26, 2016

@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 !

@ngocdtph03070
Copy link
Author

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

@ngocdtph03070
Copy link
Author

I think ws framework should support http request param body

@s4cha
Copy link
Member

s4cha commented Oct 26, 2016

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

@ngocdtph03070
Copy link
Author

ngocdtph03070 commented Oct 26, 2016

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants