Debugging an API request

2019-09-10 04:10发布

I'm trying to post a user status update to the Goodreads API.

Most of the time my request returns 200 OK and does nothing. Every now and then, though, it returns 201 Created and the status is updated. When it works it's always the first time I try to make the call after running the app in iOS simulator. Subsequent calls never work.

I don't think the problem is the API itself, since the official Goodreads iOS app uses the same call and it always works.

Their API is famous for having problems with calls that include brackets in the parameters, but I can make other calls that contain brackets and they work fine, the problem is just this one.

I'm using OAuthSwift and this is my code:

    oAuth.client.post(
        "http://www.goodreads.com/user_status",//.xml",//?user_status[book_id]=6366035&user_status[page]=168",
        parameters: ["user_status[page]" : 168, "user_status[book_id]" : 6366035, "format" : "xml"],
        //headers: ["Content-Type" : "application/x-www-form-urlencoded"],
        success: {
            data, response in
            print("")
            print(response)
        },
        failure: {
            error in
            print("")
            print(error)
        }
    )

(The commented out parts are alternatives I have tried unsuccessfully.)

I'm printing the base string that gets signed and it looks the same for the calls that work and the ones that don't, except for the nonce and the timestamp, obviously.

In the headers is also included the oauth_signature, which changes every time and sometimes contains characters that are encoded by OAuthSwift, so that could account for the call working just some of the time (it could work only when the signature doesn't contain a certain character)… but I'm printing out the headers too and I don't see any patterns or any discernible difference between the headers of the calls that work and those of the calls that don't.

So now I don't know what to test anymore… I'm checking the base string and the headers for calls that work and for calls that don't and they look the same… Could anybody think of something else that changes between calls and I should check? I have no idea what could be causing this and I don't know how to debug it.

Thanks in advance,

Daniel


Edit: Very weird… I tried my request with Paw, a Mac REST client, and with Chrome's Postman extension. If I use https I get 404 on my first call, then 201 on the second, then 404 on the third, 201 on the forth and so on. It works every other time. The time it works it doesn't matter if I use http or https, it works as long as there was a failed https request just before.

So I tried doing the same in my app: I added two https calls one after the other… in my app they always return 404.

So it seems like Postman, Paw and OAuthSwift are handling the requests differently. I don't know what could be the difference between those clients… the signature base string seems to be the same for all three, the headers too… so what else could change between them?

1条回答
Juvenile、少年°
2楼-- · 2019-09-10 04:32

In the newer versions of Xcode you can only communicate with a HTTPS server. I expect Google support that so you can change the URL. Or you can edit your Info.plist file.

App Transport Security Settings > Allow Arbitrary Loads > YES

查看更多
登录 后发表回答