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

POST request returns HTML not JSON #94

Closed
brian-ogilvie opened this issue Oct 1, 2018 · 14 comments
Closed

POST request returns HTML not JSON #94

brian-ogilvie opened this issue Oct 1, 2018 · 14 comments

Comments

@brian-ogilvie
Copy link

I'm writing a simple mobile app to run internally which interacts with your password API, but when I send POST requests to the url (https://pwpush.com/p.json), the response I get back is the full HTML code for the front page of the website, not a JSON object that can be parsed. Has anyone else had this issue?

@brian-ogilvie
Copy link
Author

Xcode 10.0
Swift 4.2 (URLRequest)

@pglombardo
Copy link
Owner

Hi @broadwaycodez,

Making a POST request to that URL with curl does respond with JSON. Some examples in the Wiki.

Could you post the list of request headers that you are sending?

Possibly adding a Content-Type header of application/json might help.

@brian-ogilvie
Copy link
Author

Thanks for responding! Here is my code. I have the "application/json" header, and I get a status code of 200, which is good. But I can't find a way to parse 'data' to get the generated URL.

`let parameters = ["password[payload]": "myPassword", "password[expire_after_days]": "3", "password[expire_after_views]": "10"]

var request = URLRequest(url: "https://pwpush.com/p.json")
request.httpMethod = "POST"
request.addValue("application/json", forHTTPHeaderField: "Content-Type")

if let httpBody = try JSONSerialization.data(withJSONObject: parameters, options: []) {

request.httpBody = httpBody

let session = URLSession.shared
session.dataTask(with: request) { (data, response, error) in
    if let response = response {
        print(response)
        // Gives me a status code of 200
    }
    if let data = data {
        do {
            let json = try JSONSerialization.jsonObject(with: data, options: [])
            // I want to parse out the resulting URL to send to clients
            // Just like you do in the Javascript alert in the wiki example
        } catch {
            print("My Error: \(error)")
            // I get an error
            // data is actually a full HTML page, not parsable JSON
        }
    }
}.resume()

}`

@brian-ogilvie
Copy link
Author

That didn't display as I expected. Here's a screenshot:

screen shot 2018-10-01 at 7 26 45 am

@pglombardo
Copy link
Owner

Hrm all looks good at first glance. It doesn't appear to be a pwpush bug but I'll try running your code and a few more test scenarios. Need to take care of a few other things first but I'll post back here with results soon.

@brian-ogilvie
Copy link
Author

I missed a couple of mistakes when I copied the code over. Here's the correct version. Results are the same.
screen shot 2018-10-01 at 1 00 32 pm

@brian-ogilvie
Copy link
Author

Thanks so much for your help on this! I just wanted to check in. Were you able to get a different response using my code?

@pglombardo
Copy link
Owner

I haven't been able to run your code as I don't have a swift env setup to test on but I just had a thought... Try adding one additional header that indicates what the response should be: Accept: application/json

So you would add something like the following:

request.addValue("application/json", forHTTPHeaderField: "Accept")

This will hopefully force the response to be json.

@brian-ogilvie
Copy link
Author

That was the magic bullet! I also had to structure my parameters a little differently than the JS example on the wiki. For anyone interested, this was the final construction of my POST request:

screen shot 2018-10-08 at 11 04 44 am

@pglombardo
Copy link
Owner

pglombardo commented Oct 8, 2018 via email

@brian-ogilvie
Copy link
Author

None at all! If you want, I can also post my code for decoding the JSON response into a parseable Swift object.

@pglombardo
Copy link
Owner

I added a link to this issue as a swift example: https://github.com/pglombardo/PasswordPusher/wiki/Password-API

If you want to post that additional code for Swift parsing, by all means, it would be appreciated.

Thanks again @broadwaycodez

@brian-ogilvie
Copy link
Author

screen shot 2018-11-07 at 8 45 40 pm

Create a custom struct to receive variables from API json response. This will need to be declared as Decodable in order for the JSONDecoder to write to it.

@brian-ogilvie
Copy link
Author

screen shot 2018-11-07 at 8 54 05 pm

Simple example of how to pass the data object from API response into your custom struct. Then you can access any of the variables from the response, most importantly the urlToken, using dot notation.

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