python PUT请求405错误没有为PUT找到资源方法

时间:2014-08-21 18:41:46

标签: python python-requests put http-status-code-405

我首先搜索了我找到的所有类似的405错误帖子,但没有找到答案。

这里的不同之处在于,PUT只能从Python请求中失败,所以我真的很困惑。也许有人知道可能会发生什么?

同样的确切调用,POST来自Python。 从Python复制/粘贴的有效负载也可用于POSTMAN。 从POSTMAN中复制/粘贴PUT或POST的相同有效负载。

我在Win7上使用Python 2.7

请求的完整响应是:

INFO:requests.packages.urllib3.connectionpool:Starting new HTTPS connection (1): {{fakeservername_replaced_for_stackoverflow_post}}
No resource method found for PUT, return 405 with Allow header
javax.ws.rs.NotAllowedException: No resource method found for PUT, return 405 with Allow header
    at org.jboss.resteasy.core.registry.SegmentNode.match(SegmentNode.java:375)
    at org.jboss.resteasy.core.registry.SegmentNode.match(SegmentNode.java:114)
    at org.jboss.resteasy.core.registry.RootNode.match(RootNode.java:43)
    at org.jboss.resteasy.core.registry.RootClassNode.match(RootClassNode.java:48)
    at org.jboss.resteasy.core.ResourceMethodRegistry.getResourceInvoker(ResourceMethodRegistry.java:444)
    at org.jboss.resteasy.core.SynchronousDispatcher.getInvoker(SynchronousDispatcher.java:234)
    at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:171)
    at org.jboss.resteasy.plugins.server.servlet.ServletContainerDispatcher.service(ServletContainerDispatcher.java:220)
    at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:56)
    at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:51)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
    at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:738)
    at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1651)
    at com.thetransactioncompany.cors.CORSFilter.doFilter(CORSFilter.java:176)
    at com.thetransactioncompany.cors.CORSFilter.doFilter(CORSFilter.java:239)
    at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1631)
    at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:549)
    at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:143)
    at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:568)
    at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:221)
    at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1111)
    at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:478)
    at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:183)
    at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1045)
    at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:141)
    at org.eclipse.jetty.server.handler.ContextHandlerCollection.handle(ContextHandlerCollection.java:199)
    at org.eclipse.jetty.server.handler.HandlerCollection.handle(HandlerCollection.java:109)
    at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:97)
    at org.eclipse.jetty.server.Server.handle(Server.java:462)
    at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:279)
    at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:232)
    at org.eclipse.jetty.io.AbstractConnection$2.run(AbstractConnection.java:534)
    at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:607)
    at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:536)
    at java.lang.Thread.run(Thread.java:744)

Expecting value: line 1 column 1 (char 0)

Finished: errors=1
DEBUG:requests.packages.urllib3.connectionpool:"PUT /api/v1/observations HTTP/1.1" 500 3078

Python代码是: [带有一些无关的调试/打印]

def putData(_wqxobj, _interface ):
    global errors

    uri = url + u'/api/v1/%s' %_interface
    hdrs = {'Authorization': 'token ' + authToken, 'Content-Type': 'application/json'}

    payload = _wqxobj    
    print '#======================= PAYLOAD =========================== #'
    print payload
    print '#==================== ENDPAYLOAD =========================== #'

    try:
        req = requests.put(uri, data=json.dumps(payload), headers=hdrs)              
        response = req.text
        print response

        if (response == 'Token is invalid.'):
            print 'error : ' + response

        if "gaia.domain.exceptions.NotUniqueException" in response:
            return "%s is not unique!"  %_interface
        else:    
            rj = req.json()
            print rj    

            _id = rj['id']

            return _id
    except Exception, e:
        errors += 1
        #print e
        err = str(e)
        return err   

作为参考,在显示的代码中传递,复制并粘贴到POSTMAN中的有效负载确实可用作PUT:

{
'medium': 'WATER', 
'observedTime': '2014-08-20T10:00:00.000-00:00', 
'samplingLocation': {'id': '1f2df33e-790c-4065-bf90-ddf12a764039'}, 
'dataClassification': 'SENSOR', 
'resultTime': '2014-08-20T10:00:00.000-00:00', 
'observedProperty': {'id': '8effe050-c13f-4b57-9671-225523aeca77'}, 
'customId': 'observation_test_1'
}

0 个答案:

没有答案