调试API请求 [英] Debugging an API request

查看:75
本文介绍了调试API请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将用户状态更新发布到Goodreads API.

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

在大多数情况下,我的请求返回200 OK且不执行任何操作.但是,时不时地,它会返回201 Created并更新状态.当它起作用时,这始终是我在iOS模拟器中运行该应用程序后第一次尝试拨打电话.随后的通话将永远无法正常工作.

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.

我认为问题不在于API本身,因为正式的Goodreads iOS应用程序使用相同的调用,并且始终有效.

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

他们的API以在参数中包含方括号的调用时遇到问题而闻名,但是我可以进行其他包含方括号的调用,并且它们都可以正常工作,问题就出在这个问题上.

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.

我正在使用OAuthSwift,这是我的代码:

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

我正在打印已签名的基本字符串,显然,除了noncetimestamp之外,对于有效的调用和无效的调用,它们看起来都是相同的.

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.

标头中还包含oauth_signature,它每次都会更改,并且有时包含由OAuthSwift编码的字符,因此可以说明该调用仅在某些时间有效(仅当签名不起作用时才有效) '不包含某个字符)...但是我也正在打印标题,但看不到有效的调用头和无效的调用头之间的任何模式或可识别的区别.

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.

预先感谢

丹尼尔

太奇怪了……我用Mac REST客户端Paw和Chrome的Postman扩展程序尝试了我的请求.如果使用https,则我的第一个电话会收到404,第二个电话会得到201,第三个电话会得到404,第四个电话会得到201,依此类推.它每隔一段时间工作一次.可以使用的时间与我使用http还是https都没有关系,只要之前有一个失败的https请求,它就可以使用.

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.

因此,我尝试在我的应用程序中执行相同的操作:我一个接一个地添加了两个https调用……在我的应用中,它们始终返回404.

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.

因此,似乎Postman,Paw和OAuthSwift处理请求的方式有所不同.我不知道这些客户端之间的区别是什么……三个签名者的签名基本字符串似乎都是相同的,所以报头也是如此……那么它们之间还有什么可能改变?

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?

推荐答案

在较新版本的Xcode中,您只能与HTTPS服务器通信.我希望有Google支持,以便您可以更改URL.或者,您也可以编辑Info.plist文件.

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

这篇关于调试API请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆