GET请求 - 断管

时间:2015-02-02 02:12:19

标签: objective-c django

我正在使用objective-c发出GET请求。服务器在django中实现。似乎制作GET REST请求会导致异常,但我不确定哪一方导致它。

在客户端,我定义了一个处理服务器请求的实用程序类:

- (HyServerRequest *)request:(NSString *)path
                      method:(NSString *)method
                        data:(NSData *)data
                       query:(NSDictionary *)query
{
    NSString * queryString = nil;

    if (query != nil) {
        queryString = [query URLEncodedString];
        path = [path stringByAppendingString:[NSString stringWithFormat:@"?%@", queryString]];
    }

    NSURL * url = [[NSURL alloc] initWithString:path relativeToURL:[self baseURL]];
    NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL:url
                                                            cachePolicy:NSURLRequestUseProtocolCachePolicy
                                                        timeoutInterval:[self requestTimeout]];

    [request setHTTPMethod:method];
    [request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
    [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    [request setValue:[NSString stringWithFormat:@"%lu", (unsigned long)[data length]] forHTTPHeaderField:@"Content-Length"];

    if (data != nil) {
        [request setHTTPBody:data];
    }

    NSURLConnection * connection = [[NSURLConnection alloc] initWithRequest:request
                                                                   delegate:self
                                                           startImmediately:NO];

    [self setConnection:connection];

    return self;
}

然后我添加了一些实用工具方法,例如:

- (HyServerRequest *)get:(NSString *)path data:(NSData *)data query:(NSDictionary *)query
{
    return [self request:path method:@"GET" data:data query:query];
}

以下是我最终提出请求的方式:

body = [NSJSONSerialization dataWithJSONObject:dict
                                       options:0
                                         error:&error];

self.syncRequest = [HyServerRequest get:[NSString stringWithFormat:@"/api/oauth/%@/", provider]
                                   data:body
                                  query:nil];

[self.syncRequest setDelegate:self];
[self.syncRequest start];

这对POST和PUT请求一直很好,但GET请求似乎不起作用。

在服务器端,这就是我解析请求主体的方式:

def parse_body(self, request):

    # REST views default the content type to application/json, but neither should this be
    # the only supported type, nor should it be hardcoded (I would like to aim at full
    # content negotiation). I'm leaving this as is for now.
    content_type = request.META.get('CONTENT_TYPE', 'application/json')

    if content_type == 'application/json':

        try:
            content = json.loads(request.body)
        except Exception as ex:
            raise exceptions.HttpParseException()

    elif content_type == 'text/plain':
        content = request.body

    else:
        raise exceptions.HttpUnsupportedMediaType()

    return content

但我没有得到任何内容。我知道我可以使用查询字符串传递GET请求的参数,但在某些情况下,它包含一个访问令牌,我宁愿不在URL中留下那些。

没有帮助的一件事是我在服务器上遇到的错误:

[02/Feb/2015 02:07:24] "GET /api/oauth/facebook/ HTTP/1.1" 400 33
[02/Feb/2015 02:07:24] "GET /api/oauth/facebook/ HTTP/1.1" 400 33
Traceback (most recent call last):
Traceback (most recent call last):
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/wsgiref/handlers.py", line 86, in run
[02/Feb/2015 02:07:24] "GET /api/oauth/facebook/ HTTP/1.1" 400 33
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/wsgiref/handlers.py", line 86, in run
Traceback (most recent call last):
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/wsgiref/handlers.py", line 86, in run
    self.finish_response()
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/wsgiref/handlers.py", line 128, in finish_response
    self.write(data)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/wsgiref/handlers.py", line 212, in write
    self.send_headers()
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/wsgiref/handlers.py", line 270, in send_headers
    self.send_preamble()
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/wsgiref/handlers.py", line 194, in send_preamble
    'Date: %s\r\n' % format_date_time(time.time())
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.py", line 324, in write
    self.finish_response()
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/wsgiref/handlers.py", line 128, in finish_response
    self.write(data)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/wsgiref/handlers.py", line 212, in write
    self.send_headers()
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/wsgiref/handlers.py", line 270, in send_headers
    self.send_preamble()
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/wsgiref/handlers.py", line 194, in send_preamble
    'Date: %s\r\n' % format_date_time(time.time())
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.py", line 324, in write
    self.finish_response()
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/wsgiref/handlers.py", line 128, in finish_response
    self.flush()
    self.write(data)
    self.flush()
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.py", line 303, in flush
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/wsgiref/handlers.py", line 212, in write
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.py", line 303, in flush
    self._sock.sendall(view[write_offset:write_offset+buffer_size])
    self.send_headers()
    self._sock.sendall(view[write_offset:write_offset+buffer_size])
error: [Errno 32] Broken pipe
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/wsgiref/handlers.py", line 270, in send_headers
error: [Errno 32] Broken pipe
    self.send_preamble()
[02/Feb/2015 02:07:24] "GET /api/oauth/facebook/ HTTP/1.1" 500 59
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/wsgiref/handlers.py", line 194, in send_preamble
----------------------------------------
Exception happened during processing of request from ('127.0.0.1', 54945)
[02/Feb/2015 02:07:24] "GET /api/oauth/facebook/ HTTP/1.1" 500 59
    'Date: %s\r\n' % format_date_time(time.time())
Traceback (most recent call last):
----------------------------------------
Exception happened during processing of request from ('127.0.0.1', 54949)
Traceback (most recent call last):
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/SocketServer.py", line 593, in process_request_thread
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/SocketServer.py", line 593, in process_request_thread
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.py", line 324, in write
    self.flush()
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.py", line 303, in flush
    self._sock.sendall(view[write_offset:write_offset+buffer_size])
error: [Errno 32] Broken pipe
[02/Feb/2015 02:07:24] "GET /api/oauth/facebook/ HTTP/1.1" 500 59
----------------------------------------
Exception happened during processing of request from ('127.0.0.1', 54947)
Traceback (most recent call last):
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/SocketServer.py", line 593, in process_request_thread
    self.finish_request(request, client_address)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/SocketServer.py", line 334, in finish_request
    self.RequestHandlerClass(request, client_address, self)
  File "/Library/Python/2.7/site-packages/django/core/servers/basehttp.py", line 129, in __init__
    super(WSGIRequestHandler, self).__init__(*args, **kwargs)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/SocketServer.py", line 651, in __init__
    self.finish()
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/SocketServer.py", line 710, in finish
    self.wfile.close()
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.py", line 279, in close
    self.flush()
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.py", line 303, in flush
    self._sock.sendall(view[write_offset:write_offset+buffer_size])
error: [Errno 32] Broken pipe
----------------------------------------
    self.finish_request(request, client_address)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/SocketServer.py", line 334, in finish_request
    self.RequestHandlerClass(request, client_address, self)
  File "/Library/Python/2.7/site-packages/django/core/servers/basehttp.py", line 129, in __init__
    super(WSGIRequestHandler, self).__init__(*args, **kwargs)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/SocketServer.py", line 651, in __init__
    self.finish()
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/SocketServer.py", line 710, in finish
    self.wfile.close()
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.py", line 279, in close
    self.flush()
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.py", line 303, in flush
    self._sock.sendall(view[write_offset:write_offset+buffer_size])
error: [Errno 32] Broken pipe
----------------------------------------
    self.finish_request(request, client_address)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/SocketServer.py", line 334, in finish_request
    self.RequestHandlerClass(request, client_address, self)
  File "/Library/Python/2.7/site-packages/django/core/servers/basehttp.py", line 129, in __init__
    super(WSGIRequestHandler, self).__init__(*args, **kwargs)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/SocketServer.py", line 651, in __init__
    self.finish()
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/SocketServer.py", line 710, in finish
    self.wfile.close()
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.py", line 279, in close
    self.flush()
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.py", line 303, in flush
    self._sock.sendall(view[write_offset:write_offset+buffer_size])
error: [Errno 32] Broken pipe
----------------------------------------

虽然URL似乎被多次调用(甚至使用不同的状态代码),但我只发出一次请求。在使用django中的流式响应之前,我遇到了这个破管问题,所以我不确定它与我现在正在做的事情有什么关系。

0 个答案:

没有答案