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

Request: fix parameters issue of POST request #35

Merged
merged 3 commits into from
Dec 7, 2016

Conversation

bigyelow
Copy link
Contributor

@bigyelow bigyelow commented Dec 6, 2016

fix POST request missing parameter bug

@lincode

// Remove query from url because RXRHTTPRequestSerializer will add all the parameters through
// `requestBySerializingRequest:withParameters:error:` method.
NSURLComponents *comp = [[NSURLComponents alloc] initWithURL:mutableRequest.URL resolvingAgainstBaseURL:NO];
comp.query = nil;
Copy link
Contributor

@lincode lincode Dec 7, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这会把 request 本来就有的 parameter 都去掉了,然后只添上我们要加上的?

comp.query = nil;
mutableRequest.URL = comp.URL;

return [[RXRHTTPRequestSerializer serializer] requestBySerializingRequest:[mutableRequest copy]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个 copy 没有必要。大麦和我在上一个 pr 里都表达了这个意见:那份 AFNetworking 的代码已经拷到这个项目里了。代码是开源的,可以视为这份代码就是你自己写的。你清楚地知道它做了什么。那么为何还要视其为黑盒呢?你知道进入那个函数就会做一次拷贝。为何还要在函数外为参数做一次拷贝呢?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

嗯,我改下吧

@bigyelow
Copy link
Contributor Author

bigyelow commented Dec 7, 2016

@lincode done.

// object from the parameters every time when it decorates a request. If we don't remove query from URL, request may
// contain duplicated query string if the original request is decorated more than 2 times.
NSURLComponents *comp = [[NSURLComponents alloc] initWithURL:mutableRequest.URL resolvingAgainstBaseURL:NO];
comp.query = nil;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

正常应该只是添加我们要加的,不动原来的 query 内容。
这会把之前在 query 里的都抹去,然后添上我们要加上的吗?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

不是,其实这里和你之前的做法一样的。原来的 query 在 https://github.com/douban/rexxar-ios/pull/35/files#diff-33436d4b2f5031181bc1ff91ba27c914R68 这一行已经加到 parameters 里了。

之前的写法是:

  NSString *query = [NSURL rxr_queryFromDictionary:parametersEncoded];
  if (query) {
    NSURLComponents *urlComps = [NSURLComponents componentsWithURL:request.URL resolvingAgainstBaseURL:YES];
    urlComps.query = query;
    request.URL = urlComps.URL;
  }

所以对 request.URL 也是用了一个新的 query, 这个 query 是通过将原来的 query 添加到 parameters(这个 parameters 包含我们要添加的参数)。

原来的做法是不修改 request 只修改 request.URL,但是现在使用了 requestSerializer 是使用新的 request,所以每次 decorate 的时候需要去掉 request.URL 里的 query,因为这部分已经添加到 parameters 里了,新生成 request 的时候自然会加进去。

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

看了之前的代码,看样子,之前的应该也写错了。不能覆盖,只能添加。

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

所以,现在写的这段代码也有问题。新添加的 query 覆盖了之前已经有的 query。这是个 bug。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

你是指漏掉了之前的 query 吗?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

是的。这只是一个 decorator,添加一些 query,但不能覆盖以前已经存在的 query。之前的代码,和现在代码都有这个问题。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

没有漏掉啊,之前的 query 加到了 parameters 里面啊,然后用这个 parameters 生成新的 request. 之前也没有漏掉。
https://github.com/douban/rexxar-ios/pull/35/files#diff-33436d4b2f5031181bc1ff91ba27c914R68

@lincode
Copy link
Contributor

lincode commented Dec 7, 2016

https://github.com/douban/rexxar-ios/blob/master/Rexxar/Decorator/RXRRequestInterceptor.m#L89

这个连续做两次拷贝也请改了吧。

@@ -86,7 +86,7 @@ - (void)startLoading
if ([decorator respondsToSelector:@selector(prepareWithRequest:)]) {
[decorator prepareWithRequest:request];
}
request = [[decorator decoratedRequestFromOriginalRequest:[request copy]] mutableCopy];
request = [[decorator decoratedRequestFromOriginalRequest:request] mutableCopy];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

方法名已经告诉了你,会返回一个拷贝的对象,就像“fromOriginal” 暗示的那样,所以这个连最后一个 mutableCopy 也不用了吧?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

需要啊, request 是 NSMutableURLRequest, 方法返回的是 NSURLRequest,不 mutableCopy 会有 warning 的

@bigyelow
Copy link
Contributor Author

bigyelow commented Dec 7, 2016

@lincode 还有问题吗?求合

@lincode lincode merged commit 615ff1b into douban:master Dec 7, 2016
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

Successfully merging this pull request may close these issues.

2 participants