I am trying to manipulate JSON request in WSO2 APIM using Javascript and sending it as application/json. I have modified axis2.xml & axis2_blocking_client.xml to include JsonStreamFormatter and JsonStreamBuilder as well. While setting mc.setPayloadJSON getting org.mozilla.javascript.WrappedException.
Below are the code snippet and error logs:
API snippet(carbon.xml):
<api context="/TestVDB/v1.0" name="creator--testVDB" version="v1.0" version-type="context">
<resource faultSequence="fault" methods="POST" url-mapping="/VcVDB">
<inSequence>
<filter regex="PRODUCTION" source="$ctx:AM_KEY_TYPE">
<then>
<property action="remove"
name="REST_URL_POSTFIX" scope="axis2"/>
<property expression="json-eval($.)" name="JSONPayload"/>
<log level="custom">
<property
expression="get-property('JSONPayload')" name="requestJson"/>
</log>
<property name="action" value="SUBMIT"/>
<script language="js"><![CDATA[var requestJson=mc.getProperty('JSONPayload');
var requestJsonObject=JSON.parse(requestJson);
var requestJsonString=JSON.stringify(requestJsonObject);
var VDBJsonObj = new Object();
VDBJsonObj.inputJsonReq=requestJsonString;
newResponseJson=JSON.stringify(VDBJsonObj);
print(newResponseJson);
mc.setPayloadJSON(newResponseJson);]]></script>
<property name="HTTP_METHOD" scope="axis2"
type="STRING" value="POST"/>
<property name="messageType" scope="axis2" value="application/json"/>
<property
expression="get-property('SYSTEM_TIME')" name="api.ut.backendRequestTime"/>
<send>
<endpoint name="creator--testVDB_APIproductionEndpoint_0">
<http uri-template="{var.uri.host}.{var.uri.port}/FpuVDB/CallRouterView/json/CallRequestRouter"/>
</endpoint>
</send>
</then>
<else>
<sequence key="_sandbox_key_error_"/>
</else>
</filter>
</inSequence>
<outSequence>
<class name="org.wso2.carbon.apimgt.usage.publisher.APIMgtResponseHandler"/>
<send/>
</outSequence>
</resource>
<handlers>
<handler class="org.wso2.carbon.apimgt.gateway.handlers.security.CORSRequestHandler">
<property name="apiImplementationType" value="ENDPOINT"/>
</handler>
<handler class="org.wso2.carbon.apimgt.gateway.handlers.security.APIAuthenticationHandler"/>
<handler class="org.wso2.carbon.apimgt.gateway.handlers.throttling.APIThrottleHandler">
<property name="id" value="A"/>
<property name="policyKeyResource" value="gov:/apimgt/applicationdata/res-tiers.xml"/>
<property name="policyKey" value="gov:/apimgt/applicationdata/tiers.xml"/>
<property name="policyKeyApplication" value="gov:/apimgt/applicationdata/app-tiers.xml"/>
</handler>
<handler class="org.wso2.carbon.apimgt.usage.publisher.APIMgtUsageHandler"/>
<handler class="org.wso2.carbon.apimgt.usage.publisher.APIMgtGoogleAnalyticsTrackingHandler">
<property name="configKey" value="gov:/apimgt/statistics/ga-config.xml"/>
</handler>
<handler class="org.wso2.carbon.apimgt.gateway.handlers.ext.APIManagerExtensionHandler"/>
</handlers>
</api>
Error Logs:
[2016-05-10 07:49:17,340] DEBUG - headers http-incoming-6 >> POST /TestVDB/v1.0/VcVDB HTTP/1.1
[2016-05-10 07:49:17,341] DEBUG - headers http-incoming-6 >> Host: 10.178.233.87:8245
[2016-05-10 07:49:17,341] DEBUG - headers http-incoming-6 >> Connection: keep-alive
[2016-05-10 07:49:17,341] DEBUG - headers http-incoming-6 >> Content-Length: 1885
[2016-05-10 07:49:17,341] DEBUG - headers http-incoming-6 >> User-Agent: Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.94 Safari/537.36
[2016-05-10 07:49:17,341] DEBUG - headers http-incoming-6 >> Cache-Control: no-cache
[2016-05-10 07:49:17,341] DEBUG - headers http-incoming-6 >> Origin: chrome-extension://fhbjgbiflinjbdggehcddcbncdddomop
[2016-05-10 07:49:17,341] DEBUG - headers http-incoming-6 >> Content-Type: application/json
[2016-05-10 07:49:17,341] DEBUG - headers http-incoming-6 >> Authorization: Bearer b51763484717d618473cc9662b237504
[2016-05-10 07:49:17,342] DEBUG - headers http-incoming-6 >> Postman-Token: 9ac93374-5fc7-3f01-1c2d-c23b4fefdecf
[2016-05-10 07:49:17,342] DEBUG - headers http-incoming-6 >> Accept: */*
[2016-05-10 07:49:17,342] DEBUG - headers http-incoming-6 >> Accept-Encoding: gzip, deflate
[2016-05-10 07:49:17,342] DEBUG - headers http-incoming-6 >> Accept-Language: en-US,en;q=0.8
[2016-05-10 07:49:17,350] INFO - LogMediator requestJson = {
"#context": "__http://etc.abc.com/metadata/context.jsonld",
"#id": "__http://etc.abc.com/message/id/abc/Router/327a3063-ab17-48c5-e053-336edb20aae9",
"#type": "msg:Message",
"msg:format": "__http://etc.abc.com/data/voc/Formats/MessageFormat-1",
"msg:type": "__http://etc.abc.com/data/voc/MessageTypes/ServiceCall-1",
"msg:from": "__http://etc.abc.com/data/voc/Contributors/abc/pqr",
"msg:to": "__http://etc.abc.com/data/voc/Contributors/abc/VirusCheck",
"msg:service": {
"#id": "__http://etc.abc.com/message/id/abc/Router/327a3063-ab18-48c5-e053-336edb20aae9",
"#type": "msg:ServiceCall",
"svc:time": "2016-05-10T05:52:05.029Z",
"svc:type": "__http://etc.abc.com/data/voc/ServiceCall/PerformVirusCheck",
"svc:about": "cert-s3-ucmdata-pqr",
"svc:resource": [
"https://s3-ucmdata-pqr.s3.amazonaws.com/EU_WEST_1-cert-s3-ucmdata-pqr-16f817e9f8eb07db4beb70e5d021d13c-63503_58484_99949.pdf?response-content-disposition=attachment%3B%20filename%3D%22EU_WEST_1-cert-s3-ucmdata-pqr-16f817e9f8eb07db4beb70e5d021d13c-63503_58484_99949.pdf%22&response-content-type=application%2Fpdf&AWSAccessKeyId=AKIAJTIYS75JIN6ZWRSQ&Expires=1473241925&Signature=cKyblhLqxS7KAH6k8Okca5BEjyk%3D"
],
"svc:params": [
{
"#context": {
"virus": "__http://etc.abc.com/data/ns/messages/abc/VirusCheckService-1/"
},
"virus:post-result-at": "https://s3-ucmdata-pqr.s3.amazonaws.com/VIRUS_EU_WEST_1-cert-s3-ucmdata-pqr-16f817e9f8eb07db4beb70e5d021d13c-63503_58484_99949.pdf?AWSAccessKeyId=FDSJTIYS75JIN6ZWRSQ&Expires=1473241925&Signature=ev44nf8MA4%2Fkfa9IRmonO8ANpMk%3D",
"virus:remove-file-properties": true,
"virus:and-again-please": null
}
]
},
"#timestamp": "2016-05-10T05:52:05.029Z"
}
{"inputJsonReq":"{\"#context\":\"__http://etc.abc.com/metadata/context.jsonld\",\"#id\":\"__http://etc.abc.com/message/id/abc/Router/327a3063-ab17-48c5-e053-336edb20aae9\",\"#type\":\"msg:Message\",\"msg:format\":\"__http://etc.abc.com/data/voc/Formats/MessageFormat-1\",\"msg:type\":\"__http://etc.abc.com/data/voc/MessageTypes/ServiceCall-1\",\"msg:from\":\"__http://etc.abc.com/data/voc/Contributors/abc/pqr\",\"msg:to\":\"__http://etc.abc.com/data/voc/Contributors/abc/VirusCheck\",\"msg:service\":{\"#id\":\"__http://etc.abc.com/message/id/abc/Router/327a3063-ab18-48c5-e053-336edb20aae9\",\"#type\":\"msg:ServiceCall\",\"svc:time\":\"2016-05-10T05:52:05.029Z\",\"svc:type\":\"__http://etc.abc.com/data/voc/ServiceCall/PerformVirusCheck\",\"svc:about\":\"cert-s3-ucmdata-pqr\",\"svc:resource\":[\"https://cert-s3-ucmdata-pqr.s3.amazonaws.com/EU_WEST_1-cert-s3-ucmdata-pqr-16f817e9f8eb07db4beb70e5d021d13c-63503_58484_99949.pdf?response-content-disposition=attachment%3B%20filename%3D%22EU_WEST_1-cert-s3-ucmdata-pqr-16f817e9f8eb07db4beb70e5d021d13c-63503_58484_99949.pdf%22&response-content-type=application%2Fpdf&AWSAccessKeyId=AKIAJABCS75JIN6ZWRSQ&Expires=1473241925&Signature=cKyblhLqxS7KAH6k8njgf5BEjyk%3D\"],\"svc:params\":[{\"#context\":{\"virus\":\"__http://etc.abc.com/data/ns/messages/abc/VirusCheckService-1/\"},\"virus:post-result-at\":\"https://cert-s3-ucmdata-pqr.s3.amazonaws.com/VIRUS_EU_WEST_1-cert-s3-ucmdata-pqr-16f817eop8b07db4beb70e5d021d13c-63503_58484_99949.pdf?AWSAccessKeyId=AKIAJTXCV75JIN6ZWRSQ&Expires=1473241925&Signature=ev44nf8MA4%2Fkfa9IRdayO8ANpMk%3D\",\"virus:remove-file-properties\":true,\"virus:and-again-please\":null}]},\"#timestamp\":\"2016-05-10T05:52:05.029Z\"}"}
[2016-05-10 07:49:17,364] ERROR - ScriptMediator The script engine returned an error executing the inlined js script function mediate
com.sun.phobos.script.util.ExtendedScriptException: org.mozilla.javascript.WrappedException: Wrapped java.lang.IllegalStateException: Expected attribute value (<Unknown Source>#8) in <Unknown Source> at line number 8
at com.sun.phobos.script.javascript.RhinoCompiledScript.eval(RhinoCompiledScript.java:68)
at javax.script.CompiledScript.eval(CompiledScript.java:92)
at org.apache.synapse.mediators.bsf.ScriptMediator.mediateForInlineScript(ScriptMediator.java:306)
at org.apache.synapse.mediators.bsf.ScriptMediator.invokeScript(ScriptMediator.java:245)
at org.apache.synapse.mediators.bsf.ScriptMediator.mediate(ScriptMediator.java:213)
at org.apache.synapse.mediators.AbstractListMediator.mediate(AbstractListMediator.java:81)
at org.apache.synapse.mediators.AbstractListMediator.mediate(AbstractListMediator.java:48)
at org.apache.synapse.mediators.filters.FilterMediator.mediate(FilterMediator.java:166)
at org.apache.synapse.mediators.AbstractListMediator.mediate(AbstractListMediator.java:81)
at org.apache.synapse.mediators.AbstractListMediator.mediate(AbstractListMediator.java:48)
at org.apache.synapse.mediators.base.SequenceMediator.mediate(SequenceMediator.java:155)
at org.apache.synapse.rest.Resource.process(Resource.java:297)
at org.apache.synapse.rest.API.process(API.java:378)
at org.apache.synapse.rest.RESTRequestHandler.dispatchToAPI(RESTRequestHandler.java:86)
at org.apache.synapse.rest.RESTRequestHandler.process(RESTRequestHandler.java:65)
at org.apache.synapse.core.axis2.Axis2SynapseEnvironment.injectMessage(Axis2SynapseEnvironment.java:295)
at org.apache.synapse.core.axis2.SynapseMessageReceiver.receive(SynapseMessageReceiver.java:83)
at org.apache.axis2.engine.AxisEngine.receive(AxisEngine.java:180)
at org.apache.synapse.transport.passthru.ServerWorker.processNonEntityEnclosingRESTHandler(ServerWorker.java:317)
at org.apache.synapse.transport.passthru.ServerWorker.processEntityEnclosingRequest(ServerWorker.java:363)
at org.apache.synapse.transport.passthru.ServerWorker.run(ServerWorker.java:142)
at org.apache.axis2.transport.base.threads.NativeWorkerPool$1.run(NativeWorkerPool.java:172)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
at java.lang.Thread.run(Thread.java:722)
Caused by: org.mozilla.javascript.WrappedException: Wrapped java.lang.IllegalStateException: Expected attribute value (<Unknown Source>#8)
at org.mozilla.javascript.Context.throwAsScriptRuntimeEx(Context.java:1754)
at org.mozilla.javascript.MemberBox.invoke(MemberBox.java:148)
at org.mozilla.javascript.NativeJavaMethod.call(NativeJavaMethod.java:225)
at org.mozilla.javascript.optimizer.OptRuntime.call1(OptRuntime.java:32)
at org.mozilla.javascript.gen._Unknown_Source__91._c_script_0(<Unknown Source>:8)
at org.mozilla.javascript.gen._Unknown_Source__91.call(<Unknown Source>)
at org.mozilla.javascript.ContextFactory.doTopCall(ContextFactory.java:394)
at org.mozilla.javascript.ScriptRuntime.doTopCall(ScriptRuntime.java:3091)
at org.mozilla.javascript.gen._Unknown_Source__91.call(<Unknown Source>)
at org.mozilla.javascript.gen._Unknown_Source__91.exec(<Unknown Source>)
at com.sun.phobos.script.javascript.RhinoCompiledScript.eval(RhinoCompiledScript.java:55)
... 24 more
Caused by: java.lang.IllegalStateException: Expected attribute value
at org.apache.synapse.commons.staxon.core.json.JsonXMLStreamReader.consumeName(JsonXMLStreamReader.java:117)
at org.apache.synapse.commons.staxon.core.json.JsonXMLStreamReader.consume(JsonXMLStreamReader.java:131)
at org.apache.synapse.commons.staxon.core.json.JsonXMLStreamReader.consume(JsonXMLStreamReader.java:162)
at org.apache.synapse.commons.staxon.core.json.JsonXMLStreamReader.consume(JsonXMLStreamReader.java:149)
at org.apache.synapse.commons.staxon.core.json.JsonXMLStreamReader.consume(JsonXMLStreamReader.java:132)
at org.apache.synapse.commons.staxon.core.base.AbstractXMLStreamReader.hasNext(AbstractXMLStreamReader.java:446)
at org.apache.synapse.commons.staxon.core.base.AbstractXMLStreamReader.next(AbstractXMLStreamReader.java:456)
at javax.xml.stream.util.StreamReaderDelegate.next(StreamReaderDelegate.java:88)
at org.apache.axiom.om.impl.builder.StAXOMBuilder.parserNext(StAXOMBuilder.java:681)
at org.apache.axiom.om.impl.builder.StAXOMBuilder.next(StAXOMBuilder.java:214)
at org.apache.axiom.om.impl.llom.OMElementImpl.buildNext(OMElementImpl.java:653)
at org.apache.axiom.om.impl.llom.OMNodeImpl.getNextOMSibling(OMNodeImpl.java:122)
at org.apache.axiom.om.impl.llom.OMElementImpl.getNextOMSibling(OMElementImpl.java:343)
at org.apache.axiom.om.impl.traverse.OMChildrenIterator.getNextNode(OMChildrenIterator.java:36)
at org.apache.axiom.om.impl.traverse.OMAbstractIterator.hasNext(OMAbstractIterator.java:58)
at org.apache.synapse.commons.json.JsonUtil.removeIndentations(JsonUtil.java:414)
at org.apache.synapse.commons.json.JsonUtil.removeIndentations(JsonUtil.java:421)
at org.apache.synapse.commons.json.JsonUtil.removeIndentations(JsonUtil.java:421)
at org.apache.synapse.commons.json.JsonUtil.newJsonPayload(JsonUtil.java:530)
at org.apache.synapse.commons.json.JsonUtil.newJsonPayload(JsonUtil.java:599)
at org.apache.synapse.mediators.bsf.ScriptMessageContext.setPayloadJSON(ScriptMessageContext.java:628)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at org.mozilla.javascript.MemberBox.invoke(MemberBox.java:126)
... 33 more
[
[2016-05-10 07:49:17,375] INFO - LogMediator STATUS = Executing default 'fault' sequence, ERROR_CODE = 0, ERROR_MESSAGE = The script engine returned an error executing the inlined js script function mediate
[2016-05-10 07:49:17,384] DEBUG - headers http-incoming-6 << HTTP/1.1 500 Internal Server Error
[2016-05-10 07:49:17,385] DEBUG - headers http-incoming-6 << Accept-Language: en-US,en;q=0.8
[2016-05-10 07:49:17,385] DEBUG - headers http-incoming-6 << Access-Control-Allow-Headers: authorization,Access-Control-Allow-Origin,Content-Type
[2016-05-10 07:49:17,385] DEBUG - headers http-incoming-6 << Access-Control-Allow-Origin: *
[2016-05-10 07:49:17,385] DEBUG - headers http-incoming-6 << Accept-Encoding: gzip, deflate
[2016-05-10 07:49:17,386] DEBUG - headers http-incoming-6 << Origin: chrome-extension://fhbjgbiflinjbdggehcddcbncdddomop
[2016-05-10 07:49:17,386] DEBUG - headers http-incoming-6 << Postman-Token: 9ac93374-5fc7-3f01-1c2d-c23b4fefdecf
[2016-05-10 07:49:17,386] DEBUG - headers http-incoming-6 << Access-Control-Allow-Methods: POST
[2016-05-10 07:49:17,386] DEBUG - headers http-incoming-6 << Content-Type: application/xml; charset=UTF-8
[2016-05-10 07:49:17,386] DEBUG - headers http-incoming-6 << Cache-Control: no-cache
[2016-05-10 07:49:17,386] DEBUG - headers http-incoming-6 << Date: Tue, 10 May 2016 11:49:17 GMT
[2016-05-10 07:49:17,386] DEBUG - headers http-incoming-6 << Transfer-Encoding: chunked
[2016-05-10 07:49:17,386] DEBUG - headers http-incoming-6 << Connection: keep-alive
When we call mc.setPayloadJSON(newResponseJson); it tries to build the existing payload in the message context and convert it to XML format before setting the modified payload. The conversion is done by the StAXON library [1].
StAXON considers "#" fields as attributes in XML when it does its conversion. [2] [3]
For Example:
{"node": {"#attribute": "myAttrib"}} is represented as
With respect to StAXON there are two problems in your payload as you already identified too.
It has the following Object with an # fields; the StAXON library does not support it as we cannot set objects inside an XML attribute.
An # field is defined after defining normal fields of a JSON object ("#timestamp": "2016-05-10T05:52:05.029Z"). StAXON also identifies this as problem. Because, in XML, we do not specify attributes after we started defining its child/text fields.
In order to mitigate this issue, we need to avoid building existing message and transformation into XML. For that we need to set following property to remove the existing payload from the message context before calling mc.setPayloadJSON(newResponseJson);
mc.getEnvelope().getBody().getFirstElement().detach();
Related
I'd like to get a SOAP response from https://ec.europa.eu/taxation_customs/vies/checkVatTestService.wsdl, using the below example XML request:
library(RCurl)
xml.request = r'[<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" >
<soapenv:Header/>
<soapenv:Body>
<urn:checkVat xmlns:urn="urn:ec.europa.eu:taxud:vies:services:checkVat:types">
<urn:countryCode>NL</urn:countryCode>
<urn:vatNumber>800938495B01</urn:vatNumber>
</urn:checkVat>
</soapenv:Body>
</soapenv:Envelope>]'
myheader=c(Connection="close",
'Content-Type' = "application/xml",
'Content-length' =nchar(xml.request),
Accept = "multipart/*",
Accept = "text/xml")
data = getURL(url = "https://ec.europa.eu/taxation_customs/vies/checkVatTestService.wsdl",
postfields=xml.request,
httpheader=myheader,
verbose=TRUE)
I am getting this error, thanks for any help:
* Trying 2a01:7080:14:100::666:30:443...
* Connected to ec.europa.eu (2a01:7080:14:100::666:30) port 443 (#0)
* schannel: disabled automatic use of client certificate
* schannel: added 155 certificate(s) from CA file 'C:/Users/XX/AppData/Local/R/win-library/4.2/RCurl/etc/ca-bundle.crt'
* schannel: connection hostname (ec.europa.eu) did not match against certificate name (*.ec.europa.eu)
* schannel: connection hostname (ec.europa.eu) validated against certificate name (ec.europa.eu)
> POST /taxation_customs/vies/checkVatTestService.wsdl HTTP/1.1
Host: ec.europa.eu
Connection: close
Content-Type: application/xml
Content-length: 379
Accept: multipart/*
Accept: text/xml
* Mark bundle as not supporting multiuse
< HTTP/1.1 405 Method Not Allowed
< Cache-Control: no-store
< Date: Mon, 29 Aug 2022 16:38:23 GMT
< Content-Length: 43
< Content-Type: text/html; charset=UTF-8
< Allow: GET, HEAD
< X-Content-Type-Options: nosniff
< X-Frame-Options: DENY
< Server: Europa
< Connection: close
<
* Closing connection 0
* schannel: shutting down SSL/TLS connection with ec.europa.eu port 443
EDIT: Second method:
r <- POST("https://ec.europa.eu/taxation_customs/vies/checkVatTestService.wsdl", body = body)
stop_for_status(r)
Error: Method Not Allowed (HTTP 405).
content(r)
[1] <body><p>Request method 'POST' not supported</p></body>
The WSDL give the definition of the SOAP endpoint. That's not where you should be posting your request. In the content of the WSDL there is a <wsdlsoap:address location=""> attribute which appears to give the correct URL where you should send your post request. This version of the request seems towk work
library(RCurl)
xml.request = r'[<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" >
<soapenv:Header/>
<soapenv:Body>
<urn:checkVat xmlns:urn="urn:ec.europa.eu:taxud:vies:services:checkVat:types">
<urn:countryCode>NL</urn:countryCode>
<urn:vatNumber>800938495B01</urn:vatNumber>
</urn:checkVat>
</soapenv:Body>
</soapenv:Envelope>]'
myheader=c(Connection="close",
'Content-Type' = "text/xml",
Accept = "text/xml")
getURL(url = "http://ec.europa.eu/taxation_customs/vies/services/checkVatTestService",
postfields=xml.request,
httpheader=myheader,
verbose=TRUE)
# [1] "<env:Envelope xmlns:env=\"http://schemas.xmlsoap.org/soap/envelope/\">
# <env:Header/><env:Body><ns2:checkVatResponse
# xmlns:ns2=\"urn:ec.europa.eu:taxud:vies:services:checkVat:types\">
# <ns2:countryCode>NL</ns2:countryCode>
# <ns2:vatNumber>800938495B01</ns2:vatNumber><ns2:requestDate>2022-08-
# 29+02:00</ns2:requestDate><ns2:valid>false</ns2:valid><ns2:name></ns2:name>
# <ns2:address></ns2:address></ns2:checkVatResponse></env:Body></env:Envelope>"
i try use sendgridr from this topic How I can add cc when sending email from R with SendGrid
and I have problem with HTTP error.
in the result I've got:
POST /v3/mail/send HTTP/1.1
Host: api.sendgrid.com
User-Agent: libcurl/7.64.1 r-curl/4.3.2 httr/1.4.2
Accept-Encoding: deflate, gzip
Accept: application/json, text/xml, application/xml, */*
Authorization: Bearer SG.Iu.............xxxx....
Content-Type: application/json
Content-Length: 470
>> {"personalizations":[{
>> "to": [ {"email": "mymail#mail.com"}],
>> "cc": [ {"email": "mymail#mail.com"}]
>> }],
>> "from": {"email": "mymail#mail.com"},
>> "subject": "Testing Sendgrid",
>> "content": [{"type": "text/plain", "value": "
>> Dear friend,
>>
>>
>>
>> I'm testing email.
>>
>>
>>
>> Kind regards,
>>
>> Mati"}]
>> }
HTTP/1.1 400 Bad Request
Server: nginx
Date: Wed, 29 Jun 2022 09:13:15 GMT
Content-Type: application/json
Content-Length: 63
Connection: keep-alive
Access-Control-Allow-Origin: https://sendgrid.api-docs.io
Access-Control-Allow-Methods: POST
Access-Control-Allow-Headers: Authorization, Content-Type, On-behalf-of, x-sg-elas-acl
Access-Control-Max-Age: 600
X-No-CORS-Reason: https://sendgrid.com/docs/Classroom/Basics/API/cors.html
Strict-Transport-Security: max-age=600; includeSubDomains
Kind regards
Mat
When press plus bottom for add more than 1 of product to shopping cart on WordPress, Finally product add to cart after one minute, but I see this error in console:
Request URL: https://abcds.com/cart/
Request Method: POST
Status Code: 503 Backend fetch failed
in my vcl I add this value but not work:
# The code below makes sure the AJAX "add to cart" function works
set req.url = regsub(req.url, "add-to-cart=\d+_\d+&", "");
# Do not cache AJAX requests.
if (req.http.X-Requested-With == "XMLHttpRequest") {
return(pass);
}
# Post requests will not be cached
if (req.http.Authorization || req.method == "POST") {
return (pass);
}
# also in vcl_backend_response I add this codes:
if (!(bereq.url ~ "wp-(login|admin)|login|logged-in|my-account|wc-api|resetpass|admin-ajax.php|arisn") && !(bereq.http.cookie ~ "wordpress_logged_in|woocommerce_items_in_cart|resetpass|wp_woocommerce_session_[a-zA-Z0-9]+|wordpress_logged_in_|comment_author|PHPSESSID|woocommerce_cart_hash") && !(beresp.status == 302))
{
unset beresp.http.set-cookie;
set beresp.ttl = 2h;
}
Related images links:
https://i.postimg.cc/hGPTFx0S/header.png
https://i.postimg.cc/BbQ7ztwk/cookies.png
update:
Varnish log:
<< Request >> 20751307
Begin req 20751306 rxreq
Timestamp Start: 1650725983.556894 0.000000 0.000000
Timestamp Req: 1650725983.556894 0.000000 0.000000
ReqStart 127.0.0.1 55316 a0
ReqMethod POST
ReqURL /cart/
ReqProtocol HTTP/1.0
ReqHeader X-Real-IP: 31.14.152.7
ReqHeader X-Forwarded-For: 31.14.152.7
ReqHeader X-Forwarded-Proto: https
ReqHeader X-Nginx: on
ReqHeader Host: bankketab.com
ReqHeader Connection: close
ReqHeader Content-Length: 297
ReqHeader sec-ch-ua: " Not A;Brand";v="99", "Chromium";v="99", "Opera";v="85"
ReqHeader accept: text/html, */*; q=0.01
ReqHeader content-type: application/x-www-form-urlencoded; charset=UTF-8
ReqHeader x-requested-with: XMLHttpRequest
ReqHeader sec-ch-ua-mobile: ?0
ReqHeader user-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.84 Safari/537.36 OPR/85.0.4341.75
ReqHeader sec-ch-ua-platform: "Windows"
ReqHeader origin: https://bankketab.com
ReqHeader sec-fetch-site: same-origin
ReqHeader sec-fetch-mode: cors
ReqHeader sec-fetch-dest: empty
ReqHeader referer: https://bankketab.com/cart/
ReqHeader accept-encoding: gzip, deflate, br
ReqHeader accept-language: en-US,en;q=0.9
ReqHeader cookie: PHPSESSID=8beb52490b9f7ad5456f8b064a9439a5; woocommerce_recently_viewed=20077%7C20092%7C19496%7C20136; digits_countrycode=98; woocommerce_items_in_cart=1; wp_woocommerce_session_d373d61d3cf9a04133a8550c98d33197=t_df9a955c5d0c76b7f6a9ce2add86d6%7C
ReqUnset X-Forwarded-For: 31.14.152.7
ReqHeader X-Forwarded-For: 31.14.152.7, 127.0.0.1
VCL_call RECV
ReqUnset Host: bankketab.com
ReqHeader host: bankketab.com
ReqURL /cart/
VCL_return pass
VCL_call HASH
VCL_return lookup
VCL_call PASS
VCL_return fetch
Link bereq 20751308 pass
Storage malloc Transient
Timestamp ReqBody: 1650725983.557004 0.000110 0.000110
VCL_Error Uncached req.body can only be consumed once.
Timestamp Fetch: 1650726043.823773 60.266879 60.266769
RespProtocol HTTP/1.1
RespStatus 503
RespReason Backend fetch failed
RespHeader Date: Sat, 23 Apr 2022 15:00:43 GMT
RespHeader Server: Varnish
RespHeader Content-Type: text/html; charset=utf-8
RespHeader Retry-After: 5
RespHeader X-Varnish: 20751307
RespHeader Age: 0
RespHeader Via: 1.1 varnish (Varnish/6.0)
VCL_call DELIVER
RespHeader X-status: 0
RespUnset X-Varnish: 20751307
RespUnset Age: 0
RespUnset Via: 1.1 varnish (Varnish/6.0)
RespHeader X-Configured-By: ServerSetup.co
RespUnset Server: Varnish
RespHeader Server: Nitro
VCL_return deliver
Timestamp Process: 1650726043.823791 60.266897 0.000018
RespHeader Content-Length: 285
RespHeader Connection: close
Timestamp Resp: 1650726043.823839 60.266946 0.000049
ReqAcct 1166 297 1463 231 285 516
End
** << BeReq >> 20751308
-- Begin bereq 20751307 pass
-- Timestamp Start: 1650725983.556958 0.000000 0.000000
-- BereqMethod POST
-- BereqURL /cart/
-- BereqProtocol HTTP/1.0
-- BereqHeader X-Real-IP: 31.14.152.7
-- BereqHeader X-Forwarded-Proto: https
-- BereqHeader X-Nginx: on
-- BereqHeader Content-Length: 297
-- BereqHeader sec-ch-ua: " Not A;Brand";v="99", "Chromium";v="99", "Opera";v="85"
-- BereqHeader accept: text/html, */*; q=0.01
-- BereqHeader content-type: application/x-www-form-urlencoded; charset=UTF-8
-- BereqHeader x-requested-with: XMLHttpRequest
-- BereqHeader sec-ch-ua-mobile: ?0
-- BereqHeader user-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.84 Safari/537.36 OPR/85.0.4341.75
-- BereqHeader sec-ch-ua-platform: "Windows"
-- BereqHeader origin: https://bankketab.com
-- BereqHeader sec-fetch-site: same-origin
-- BereqHeader sec-fetch-mode: cors
-- BereqHeader sec-fetch-dest: empty
-- BereqHeader referer: https://bankketab.com/cart/
-- BereqHeader accept-encoding: gzip, deflate, br
-- BereqHeader accept-language: en-US,en;q=0.9
-- BereqHeader cookie: PHPSESSID=8beb52490b9f7ad5456f8b064a9439a5; woocommerce_recently_viewed=20077%7C20092%7C19496%7C20136; digits_countrycode=98; woocommerce_items_in_cart=1; wp_woocommerce_session_d373d61d3cf9a04133a8550c98d33197=t_df9a955c5d0c76b7f6a9ce2add86d6%7C
-- BereqHeader X-Forwarded-For: 31.14.152.7, 127.0.0.1
-- BereqHeader host: bankketab.com
-- BereqProtocol HTTP/1.1
-- BereqHeader X-Varnish: 20751308
-- VCL_call BACKEND_FETCH
-- VCL_return fetch
-- BackendOpen 50 boot.cPanelServer 144.76.9.138 8080 144.76.9.138 34358
-- BackendStart 144.76.9.138 8080
-- Timestamp Bereq: 1650725983.557008 0.000050 0.000050
-- Timestamp Beresp: 1650725983.823515 0.266558 0.266507
-- BerespProtocol HTTP/1.1
-- BerespStatus 302
-- BerespReason Found
-- BerespHeader Connection: Keep-Alive
-- BerespHeader Expires: Wed, 11 Jan 1984 05:00:00 GMT
-- BerespHeader Cache-Control: no-cache, no-store, must-revalidate, max-age=0
-- BerespHeader X-Redirect-By: WordPress
-- BerespHeader Location: https://bankketab.com/cart/
-- BerespHeader Set-Cookie: woocommerce_cart_hash=3f7d291f9814ba9163415e03a89629cf; path=/; secure
-- BerespHeader Content-Type: text/html; charset=UTF-8
-- BerespHeader Content-Length: 0
-- BerespHeader Date: Sat, 23 Apr 2022 14:59:43 GMT
-- VCL_call BACKEND_RESPONSE
-- BerespHeader Vary: User-Agent
-- VCL_return retry
-- BackendReuse 50 boot.cPanelServer
-- Timestamp Retry: 1650725983.823536 0.266578 0.000021
-- Link bereq 19813441 retry
-- End
*** << BeReq >> 19813441
--- Begin bereq 20751308 retry
--- Timestamp Start: 1650725983.823536 0.266578 0.000000
--- BereqMethod POST
--- BereqURL /cart/
--- BereqProtocol HTTP/1.1
--- BereqHeader X-Real-IP: 31.14.152.7
--- BereqHeader X-Forwarded-Proto: https
--- BereqHeader X-Nginx: on
--- BereqHeader Content-Length: 297
--- BereqHeader sec-ch-ua: " Not A;Brand";v="99", "Chromium";v="99", "Opera";v="85"
--- BereqHeader accept: text/html, */*; q=0.01
--- BereqHeader content-type: application/x-www-form-urlencoded; charset=UTF-8
--- BereqHeader x-requested-with: XMLHttpRequest
--- BereqHeader sec-ch-ua-mobile: ?0
--- BereqHeader user-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.84 Safari/537.36 OPR/85.0.4341.75
--- BereqHeader sec-ch-ua-platform: "Windows"
--- BereqHeader origin: https://bankketab.com
--- BereqHeader sec-fetch-site: same-origin
--- BereqHeader sec-fetch-mode: cors
--- BereqHeader sec-fetch-dest: empty
--- BereqHeader referer: https://bankketab.com/cart/
--- BereqHeader accept-encoding: gzip, deflate, br
--- BereqHeader accept-language: en-US,en;q=0.9
--- BereqHeader cookie: PHPSESSID=8beb52490b9f7ad5456f8b064a9439a5; woocommerce_recently_viewed=20077%7C20092%7C19496%7C20136; digits_countrycode=98; woocommerce_items_in_cart=1; wp_woocommerce_session_d373d61d3cf9a04133a8550c98d33197=t_df9a955c5d0c76b7f6a9ce2add86d6%7C
--- BereqHeader X-Forwarded-For: 31.14.152.7, 127.0.0.1
--- BereqHeader host: bankketab.com
--- BereqHeader X-Varnish: 20751308
--- BereqUnset X-Varnish: 20751308
--- BereqHeader X-Varnish: 19813441
--- VCL_call BACKEND_FETCH
--- VCL_return fetch
--- BackendOpen 50 boot.cPanelServer 144.76.9.138 8080 144.76.9.138 34358
--- BackendStart 144.76.9.138 8080
--- FetchError backend write error: 0 (Success)
--- Timestamp Bereq: 1650725983.823596 0.266638 0.000060
--- FetchError Timed out reusing backend connection
--- BackendClose 50 boot.cPanelServer
--- Timestamp Beresp: 1650726043.823712 60.266755 60.000117
--- Timestamp Error: 1650726043.823717 60.266759 0.000004
--- BerespProtocol HTTP/1.1
--- BerespStatus 503
--- BerespReason Service Unavailable
--- BerespReason Backend fetch failed
--- BerespHeader Date: Sat, 23 Apr 2022 15:00:43 GMT
--- BerespHeader Server: Varnish
--- VCL_call BACKEND_ERROR
--- BerespHeader Content-Type: text/html; charset=utf-8
--- BerespHeader Retry-After: 5
--- VCL_return deliver
--- Storage malloc Transient
--- Length 285
--- BereqAcct 2358 297 2655 394 0 394
--- End
The request body error
As mentioned in the comments, the following log output is quite interesting:
Timestamp ReqBody: 1650725983.557004 0.000110 0.000110
VCL_Error Uncached req.body can only be consumed once.
Timestamp Fetch: 1650726043.823773 60.266879 60.266769
The Uncached req.body can only be consumed once error implies that your retrying a POST call and lose the request body in the process.
The following VCL code from your VCL file has logic that could cause this behavior:
if (beresp.http.Location == "https://" + bereq.http.host + bereq.url) {
if (bereq.retries > 1)
{
unset beresp.http.Location;
}
else {
return (retry);
}
}
The fact that the request is a POST request where the output is a 302 redirect with a Location: https://bankketab.com/cart/ header triggers that part of the VCL.
The problem with retrying a request that has a request body (such as a typical POST request), is that Varnish doesn't cache the request body. That information is lost when asking for a retry.
The HTTP 503 backend fetch failure
All of this explains the VCL_Error Uncached req.body can only be consumed once. error.
The actual HTTP 503 error could be something entirely different. However, they could be related.
The logs below refer to a backend write error:
--- BackendOpen 50 boot.cPanelServer 144.76.9.138 8080 144.76.9.138 34358
--- BackendStart 144.76.9.138 8080
--- FetchError backend write error: 0 (Success)
--- Timestamp Bereq: 1650725983.823596 0.266638 0.000060
--- FetchError Timed out reusing backend connection
--- BackendClose 50 boot.cPanelServer
I'm not 100% sure, but it could be that the backend is waiting for the POST request body. It doesn't get it, times out and returns the error.
Monitoring backend health
A backend returns an HTTP 503 error when it is not available or when that backend doesn't return the expected result in a timely fashion.
Luckily there's a varnishlog command that can help us check the status of a pre-defined health probe.
Your VCL code happens to feature a backend_healthcheck health probe that polls the backend every 5 seconds.
Please run the following command to check the general health of your backend:
varnishlog -g raw -i backend_health
Every 5 seconds there should be a new log line with the status of the health check. Run it for a minute or so and see what the status is. If the backend is healthy, there is no critical problem with the backend. This can also help us conclude that the problem you're experiencing is really related to the absence of the request body in the retry.
Solving the problem
My advice is to use the std.cache_req_body() function that is described in https://varnish-cache.org/docs/6.0/reference/vmod_generated.html#func-cache-req-body to ensure the request body services a return(retry) in the VCL code.
Here's an oversimplified VCL example that features this function:
sub vcl_recv {
if(std.cache_req_body(1KB)) {
std.log("Request body accessible");
} else {
std.log("Request body not accessible");
}
}
A boolean value is returned from std.cache_req_body() describing whether or not retrieving the request body from cache was successful. You could return a synthetic error in the else path if you want.
There's no guarantee that caching the request body will solve the issue, but there are indications in the log that the failure can be caused by the absence of the request body. In any case it makes sense to run varnishlog -g raw -i backend_health to figure out what the general health is of your backend.
I have a Java client, that is making a POST call to the v1/graphql endpoint of a Hasura server (v1.3.3)
I'm making the HTTP call using the Square okhttp3 library (v4.9.1). The data transfer is happening over HTTP1.1, using chunked transfer-encoding.
The client is failing with the following error:
Caused by: java.net.ProtocolException: unexpected end of stream
at okhttp3.internal.http1.Http1ExchangeCodec$ChunkedSource.read(Http1ExchangeCodec.kt:415) ~[okhttp-4.9.1.jar:?]
at okhttp3.internal.connection.Exchange$ResponseBodySource.read(Exchange.kt:276) ~[okhttp-4.9.1.jar:?]
at okio.RealBufferedSource.read(RealBufferedSource.kt:189) ~[okio-jvm-2.8.0.jar:?]
at okio.RealBufferedSource.exhausted(RealBufferedSource.kt:197) ~[okio-jvm-2.8.0.jar:?]
at okio.InflaterSource.refill(InflaterSource.kt:112) ~[okio-jvm-2.8.0.jar:?]
at okio.InflaterSource.readOrInflate(InflaterSource.kt:76) ~[okio-jvm-2.8.0.jar:?]
at okio.InflaterSource.read(InflaterSource.kt:49) ~[okio-jvm-2.8.0.jar:?]
at okio.GzipSource.read(GzipSource.kt:69) ~[okio-jvm-2.8.0.jar:?]
at okio.Buffer.writeAll(Buffer.kt:1642) ~[okio-jvm-2.8.0.jar:?]
at okio.RealBufferedSource.readString(RealBufferedSource.kt:95) ~[okio-jvm-2.8.0.jar:?]
at okhttp3.ResponseBody.string(ResponseBody.kt:187) ~[okhttp-4.9.1.jar:?]
Request Headers:
INFO: Content-Type: application/json; charset=utf-8
INFO: Content-Length: 1928
INFO: Host: localhost:10191
INFO: Connection: Keep-Alive
INFO: Accept-Encoding: gzip
INFO: User-Agent: okhttp/4.9.1
Response headers:
INFO: Transfer-Encoding: chunked
INFO: Date: Tue, 27 Apr 2021 12:06:39 GMT
INFO: Server: Warp/3.3.10
INFO: x-request-id: d019408e-e2e3-4583-bcd6-050d4a496b11
INFO: Content-Type: application/json; charset=utf-8
INFO: Content-Encoding: gzip
This is the client code used for the making the POST call:
private static final MediaType MEDIA_TYPE_JSON = MediaType.parse("application/json; charset=utf-8");
private static OkHttpClient okHttpClient = new OkHttpClient.Builder()
.connectTimeout(30, TimeUnit.SECONDS)
.writeTimeout(5, TimeUnit.MINUTES)
.readTimeout(5, TimeUnit.MINUTES)
.addNetworkInterceptor(loggingInterceptor)
.build();
public GenericHttpResponse httpPost(String url, String textBody, GenericHttpMediaType genericMediaType) throws HttpClientException {
RequestBody body = RequestBody.create(okHttpMediaType, textBody);
Request postRequest = new Request.Builder().url(url).post(body).build();
Call postCall = okHttpClient.newCall(okHttpRequest);
Response postResponse = postCall.execute();
return GenericHttpResponse
.builder()
.body(okHttpResponse.body().string())
.headers(okHttpResponse.headers().toMultimap())
.code(okHttpResponse.code())
.build();
}
This failure is only happening for large response sizes. As per the server logs, the response size (after gzip encoding) is around 52MB, but the call is still failing. This same code has been working fine for response sizes around 10-15MB.
I tried replicating the same issue through a simple cURL call, but that ran successfully:
curl -v -s --request POST 'http://<hasura_endpoint>/v1/graphql' \
--header 'Content-Type: application/json' \
--header 'Accept-Encoding: gzip, deflate, br' \
--data-raw '...'
* Trying ::1...
* TCP_NODELAY set
* Connected to <host> (::1) port <port> (#0)
> POST /v1/graphql HTTP/1.1
> Host: <host>:<port>
> User-Agent: curl/7.64.1
> Accept: */*
> Content-Type: application/json
> Accept-Encoding: gzip, deflate, br
> Content-Length: 1840
> Expect: 100-continue
>
< HTTP/1.1 100 Continue
} [1840 bytes data]
* We are completely uploaded and fine
< HTTP/1.1 200 OK
< Transfer-Encoding: chunked
< Date: Tue, 27 Apr 2021 11:59:24 GMT
< Server: Warp/3.3.10
< x-request-id: 27e3ff3f-8b95-4328-a1bc-a5492e68f995
< Content-Type: application/json; charset=utf-8
< Content-Encoding: gzip
<
{ [6 bytes data]
* Connection #0 to host <host> left intact
* Closing connection 0
So I'm assuming that this error is specific to the Java client.
Based on suggestions provided in similar posts, I tried the following other approaches:
Adding a Connection: close header to the request
Sending Transfer-Encoding: gzip header in the request
Setting the retryOnConnectionFailure for the OkHttp client to true
But none of these approaches were able to resolve the issue.
So, my questions are:
What could be the underlying cause for this issue? Since I'm using chunked transfer encoding here, I suppose it's not due to an incorrect content-length header passed in the response.
What are the approaches I can try for debugging this further?
Would really appreciate any insights on this. Thank you.
Apache Tika should be accessible from Python program via HTTP, but I can't get it to work.
I am using this command to run the server (with and without the two options at the end):
java -jar tika-server-1.17.jar --port 5677 -enableUnsecureFeatures -enableFileUrl
And it works fine with curl:
curl -v -T /tmp/tmpsojwBN http://localhost:5677/tika
* Trying 127.0.0.1...
* Connected to localhost (127.0.0.1) port 5677 (#0)
> PUT /tika HTTP/1.1
> Host: localhost:5677
> User-Agent: curl/7.47.0
> Accept: */*
> Accept-Encoding: gzip, deflate
> Content-Length: 418074
> Expect: 100-continue
>
< HTTP/1.1 100 Continue
* We are completely uploaded and fine
< HTTP/1.1 200 OK
< Content-Type: text/plain
< Date: Sat, 07 Apr 2018 12:28:41 GMT
< Transfer-Encoding: chunked
< Server: Jetty(8.y.z-SNAPSHOT)
But when I try something like (tried different combinations for headers, here I recreated same headers as python-tika client uses):
with tempfile.NamedTemporaryFile() as tmp_file:
download_file(url, tmp_file)
payload = open(tmp_file.name, 'rb')
headers = {
'Accept': 'application/json',
'Content-Disposition': 'attachment; filename={}'.format(
os.path.basename(tmp_file.name))}
response = requests.put(TIKA_ENDPOINT_URL + '/tika', payload,
headers=headers,
verify=False)
I've tried to use payload as well as fileUrl - with the same result of WARN javax.ws.rs.ClientErrorException: HTTP 406 Not Acceptable and java stack trace on the server. Full trace:
WARN javax.ws.rs.ClientErrorException: HTTP 406 Not Acceptable
at org.apache.cxf.jaxrs.utils.SpecExceptions.toHttpException(SpecExceptions.java:117)
at org.apache.cxf.jaxrs.utils.ExceptionUtils.toHttpException(ExceptionUtils.java:173)
at org.apache.cxf.jaxrs.utils.JAXRSUtils.findTargetMethod(JAXRSUtils.java:542)
at org.apache.cxf.jaxrs.interceptor.JAXRSInInterceptor.processRequest(JAXRSInInterceptor.java:177)
at org.apache.cxf.jaxrs.interceptor.JAXRSInInterceptor.handleMessage(JAXRSInInterceptor.java:77)
at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:307)
at org.apache.cxf.transport.ChainInitiationObserver.onMessage(ChainInitiationObserver.java:121)
at org.apache.cxf.transport.http.AbstractHTTPDestination.invoke(AbstractHTTPDestination.java:274)
at org.apache.cxf.transport.http_jetty.JettyHTTPDestination.doService(JettyHTTPDestination.java:261)
at org.apache.cxf.transport.http_jetty.JettyHTTPHandler.handle(JettyHTTPHandler.java:76)
at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1088)
at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1024)
at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:135)
at org.eclipse.jetty.server.handler.ContextHandlerCollection.handle(ContextHandlerCollection.java:255)
at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:116)
at org.eclipse.jetty.server.Server.handle(Server.java:370)
at org.eclipse.jetty.server.AbstractHttpConnection.handleRequest(AbstractHttpConnection.java:494)
at org.eclipse.jetty.server.AbstractHttpConnection.headerComplete(AbstractHttpConnection.java:973)
at org.eclipse.jetty.server.AbstractHttpConnection$RequestHandler.headerComplete(AbstractHttpConnection.java:1035)
at org.eclipse.jetty.http.HttpParser.parseNext(HttpParser.java:641)
at org.eclipse.jetty.http.HttpParser.parseAvailable(HttpParser.java:231)
at org.eclipse.jetty.server.AsyncHttpConnection.handle(AsyncHttpConnection.java:82)
at org.eclipse.jetty.io.nio.SelectChannelEndPoint.handle(SelectChannelEndPoint.java:696)
at org.eclipse.jetty.io.nio.SelectChannelEndPoint$1.run(SelectChannelEndPoint.java:53)
at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:608)
at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:543)
at java.lang.Thread.run(Thread.java:748)
I've also tried to compare ( with nc -l localhost 5677 | less) what is so different with two requests (payload abbreviated):
From curl:
PUT /tika HTTP/1.1
Host: localhost:5677
User-Agent: curl/7.47.0
Accept: */*
Content-Length: 418074
Expect: 100-continue
%PDF-1.4
%<D3><EB><E9><E1>
1 0 obj
<</Creator (Chromium)
From Python requests library:
PUT /tika HTTP/1.1
Host: localhost:5677
Connection: keep-alive
Accept-Encoding: gzip, deflate
Accept: application/json
User-Agent: python-requests/2.13.0
Content-type: application/pdf
Content-Length: 246176
%PDF-1.4
%<D3><EB><E9><E1>
1 0 obj
<</Creator (Chromium)
The question is, what is the correct way to call Tika server from Python?
I've also tried python tika library in client-only mode and using tika-app via jnius. With tika client, as well as using tika-app.jar with pyjnius, I only freezes (call never returns) when I use them in a celery worker. At the same, pyjnius / tika-app and tika-python script both work nicely in a script: I have not figured out what is wrong inside celery worker. I guess, something to do with threading and/or initialization in wrong place. But that is a topic for another question.
And here is what tika-python requests:
PUT /tika HTTP/1.1
Host: localhost:5677
Connection: keep-alive
Accept-Encoding: gzip, deflate
Accept: application/json
User-Agent: python-requests/2.13.0
Content-Disposition: attachment; filename=tmpb3YkTq
Content-Length: 183234
%PDF-1.4
%<D3><EB><E9><E1>
1 0 obj
<</Creator (Chromium)
And now it seems like this is some kind of a problem with tika server:
$ tika-python --verbose --server 'localhost' --port 5677 parse all /tmp/tmpb3YkTq
2018-04-08 09:44:11,555 [MainThread ] [INFO ] Writing ./tmpb3YkTq_meta.json
(<open file '<stderr>', mode 'w' at 0x7f0b688eb1e0>, 'Request headers: ', {'Accept': 'application/json', 'Content-Disposition': 'attachment; filename=tmpb3YkTq'})
(<open file '<stderr>', mode 'w' at 0x7f0b688eb1e0>, 'Response headers: ', {'Date': 'Sun, 08 Apr 2018 06:44:13 GMT', 'Transfer-Encoding': 'chunked', 'Content-Type': 'application/json', 'Server': 'Jetty(8.y.z-SNAPSHOT)'})
['./tmpb3YkTq_meta.json']
Cf:
$ tika-python --verbose --server 'localhost' --port 5677 parse text /tmp/tmpb3YkTq
2018-04-08 09:43:38,326 [MainThread ] [INFO ] Writing ./tmpb3YkTq_meta.json
(<open file '<stderr>', mode 'w' at 0x7fc3eee4a1e0>, 'Request headers: ', {'Accept': 'application/json', 'Content-Disposition': 'attachment; filename=tmpb3YkTq'})
(<open file '<stderr>', mode 'w' at 0x7fc3eee4a1e0>, 'Response headers: ', {'Date': 'Sun, 08 Apr 2018 06:43:38 GMT', 'Content-Length': '0', 'Server': 'Jetty(8.y.z-SNAPSHOT)'})
2018-04-08 09:43:38,409 [MainThread ] [WARNI] Tika server returned status: 406
['./tmpb3YkTq_meta.json']