Fetching value of "accept-ranges" via libcurl - libcurl

I want to fetch the value of accept-ranges" via libcurl [to check the server support for the same. ]
Am thinking to perform with option CURLOPT_NOBODY and parse the response for accept-ranges.
is there any dirct get_info flag to fetch just the accept-ranges values.

"Accept-Ranges" is a usual HTTP header. You can retrieve headers in a header function.

Related

How can I send a POST request in C ++ without using sockets

I have python code that sends a POST request and gets a json, I need to rewrite it in C ++ (Windows 10, Visual Studio 2019). I don’t understand what tools can really do everything I need without complicating the code.
There will be a console application that must send a request to send or receive data, more precisely a video stream.
I read about Boost.Asio, but it seems to work only with sockets, is there any way without them? At first I wanted to use it, as the most famous. I read about сurl, but it hasn't been updated for a long time, is it still relevant?
headers_predict = {
"Content-type": "application/json;charset=UTF-8",
"Accept": "application/json",
"X-Session-ID": session_id
}
data_predict = {
"audio": {
"data": sound_base64,
"mime": "audio/pcm16"
},
"package_id": ""
}
url = 'https://cp.speechpro.com/recognize'
r = requests.post(url, headers=headers_predict,
data=json.dumps(data_predict))
print('Response: %s' % r.text)
I wouldn't want to use sockets, because I don't understand them.
I need to be able to set the header and data as a json.
sockets, is there any way without them?
Technically, HTTP does not specify the underlying transport protocol and it can work with any sort of streaming transport. You could for example write the request into a file.
But, if you currently use TCP and don't want to change that, then you must use sockets. You don't need to interact with them directly if you use an existing HTTP client library.

Postman Error: Value is not a valid byteString

I am trying to invoke a POST web service via Postman and I am getting the following error.
Error while sending request: Failed to execute setRequestHeader on
XMLHttpRequest: Value is not a valid ByteString.
Request Headers
X-PW-AccessToken:{{api_token}}
X-PW-Application:developer_api
X-PW-UserEmail:{{api_email}}
Content-Type:application/json
Request Body
{
"page_size": 25
}
Can anyone tell me why I am getting this error, and how can I get rid of this?
I think Http protocol's header can only post ByteString (what is ByteString? I think it is ASCII).
So if you have other char, for example, 汉字. if you put '汉字' add to Http Header the error 'Value is not a valid ByteString' happen!
Solove: You can use the encodeURI function to encode the String in the client, and then, You can use URLdecode.decode()(java) to decode the header information in the server.

soap connector with access token from header

I'm using the loopback-connector-soap and can pass my access token in like this:
var ds = loopback.createDataSource('soap',
{
...
,soapHeaders: ["..."+ token +"..."]
});
I'm putting a REST layer on top of this and I got it working. But 3rd parties will be hitting this API, so what I really need is to allow the third party to pass their token in via the header when they hit the REST route:
Authorization: Bearer _token_
The app will then place their token in the soap header. Does loopback's soap-connector allow for this scenario?
Things to try:
The loopback token module can be instructed to look for values in headers that you specify: http://apidocs.strongloop.com/loopback/#loopback-token
app.use(loopback.token({
cookies: ['foo-auth'],
headers: ['foo-auth', 'X-Foo-Auth'],
params: ['foo-auth', 'foo_auth']
}));
I use it myself for other scenarios (need it in my remote methods): https://github.com/ShoppinPal/warehouse/blob/master/server/server.js#L17, but if that doesn't "just work" meaning if that doesn't directly translate into the value also being set into the soap-connector automagically ...
Then perhaps you can use a middleware to take the value and set into the loopback context, to be later picked up by your soap connector? Here's some (crude) middleware code of mine: https://github.com/ShoppinPal/warehouse/blob/master/server/server.js#L18-L35
... but I wonder where you might write code for the soap-connector to pick that value out of the loopback context? Because right now the instantiation looks to be global and one time so I wonder when you get a chance again to edit it.

tastypie: PUT does not work: error cannot access body after reading from request's data stream

I'm trying django-tastypie with a REST client in my browser (Postman)
GET works well:
GET http://127.0.0.1:8000/api/v1/entry/
GET http://127.0.0.1:8000/api/v1/entry/1/
But I can't get PUT works with an entry:
PUT GET http://127.0.0.1:8000/api/v1/entry/1/
I get this error:
{
"error_message": "You cannot access body after reading from request's data stream",
...
}
I allowed the method in the resource though.
Where can it come from?
Thanks
OK, I have found a solution.
On postman, choose the 'raw' option and type the json data.
Then add a Content-Type header by clicking on the 'Headers' button on the top right. Type "application/json".

Accept headers of the clojure.xml/parse call

When calling the function clojure.xml/parse with an URI Clojure performs a HTTP GET request to fetch the data. However the HTTP request contains the following accept headers:
text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2
Shouldn't this be application/xml?
Calling clojure.xml/parse with a String parameter (URI) is similar to this java code:
SAXParserFactory.newInstance().newSAXParser().parse("<your_uri>", <instance of XMLHandler provided by Clojure>);
Clojure does not perform a HTTP GET request. It just uses SAX parser as default parser. Sax parser internally creates an instance of XMLInputSource and passes it all the way down to XMLEntityManager. Class XMLEntityManager does all the work related to opening connection and getting your xml (or more like html) document:
URL location = new URL(expandedSystemId);
URLConnection connect = location.openConnection();
... skip ...
stream = connect.getInputStream();
If XmlInputSource is an instance of HTTPInputSource, then XMLEntityManager sets up HTTP request properties. However, there is no similar functionality for XMLInputSource (which is what we have in case of SAXParser).
I guess you what might help you is changing your SAX parser to some other implementation.