How to pass cyrillic symbols with POST request with flask - json

I have such a route:
#app.route('/wikidata/api/v1.0/ask', methods=['POST'])
def get_tasks():
print(request.data)
print(request.json)
return jsonify(1)
I send a request:
curl -i -H "Content-Type:application/json" -X POST -d "{\"название\": \"значение?\",\"param1\": \"Q29424\"}" http://localhost:8529/wikidata/api/v1.0/ask
and get the error:
HTTP/1.0 400 BAD REQUEST
Content-Type: text/html
Content-Length: 223
Server: Werkzeug/0.14.1 Python/3.6.5
Date: Fri, 15 Feb 2019 08:34:27 GMT
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<title>400 Bad Request</title>
<h1>Bad Request</h1>
<p>Failed to decode JSON object: 'utf-8' codec can't decode byte 0xed in position 2: invalid continuation byte</p>
Meanwhile print(request.data) shows that request.data is b'{"\xed\xe0\xe7\xe2\xe0\xed\xe8\xe5": "\xe7\xed\xe0\xf7\xe5\xed\xe8\xe5?","param1": "Q29424"}'
The only thing that helped so far is
decoded_data = request.data.decode('windows-1251')
question = json.loads(decoded_data)
I'm looking a way to send a request properly (or configure server) so that I can use request.json without errors.
Thank you.

That's a Windows specific issue likely due to the default charset in the Windows console prompt. The cyrillic characters in your command line are misinterpreted with a non UTF-8 compatible charset.
Since you already use Python, the easiest way IMHO to send your request is to use the requests module (that you can install using pip install requests). Then type this command in a python file, using UTF-8 as charset:
import requests
requests.post("http://localhost:8529/wikidata/api/v1.0/ask", json={"название": "значение?","param1": "Q29424"})
Run it and it will have the same effect as your curl command line, only with proper cyrillic characters handling.

Related

How to serve a static JSON file from Phoenix with charset utf-8 for Filestack

I have a Phoenix app and on the javascript side I use the Filestack client. Filestack requests a JSON file from my server. I had put the file in my asset directory and it gets loaded but the Filestack Javascript client crashes with an error because it can't read the json do to german umlauts (öäü). I looked at the header and it gets served like this Content-type: application/json. I think what I need is Content-type: application/json; charset=utf-8. I also use webpack2 btw.
How do I accomplish this?
Plug.Static uses the mime package to set the content-type header. You can override the value for json as described in the mime package's README. Make sure your app is using mime version 1.1.0 or later because the builtin mime types were not overridable due to a bug that was fixed in 1.1.0.
Add this to config/config.exs:
config :mime, :types, %{"application/json; charset=utf-8" => ["json"]}
Then, force recompile mime:
mix deps.clean --build mime
and then start Phoenix:
mix phoenix.server
After this, the content-type of .json files served by Plug.Static should be application/json; charset=utf-8:
$ curl -I localhost:4000/js/foo.json
HTTP/1.1 200 OK
server: Cowboy
date: Sat, 18 Feb 2017 14:36:51 GMT
content-length: 3
cache-control: public
etag: 8EA91E
content-type: application/json; charset=utf-8

Why is wget POST giving 400 Bad Request?

I am trying to use wget to POST a .json file to a site using the REST api. Specifically I am trying to create an issue in JIRA from my command line. I am aware that the easiest way to do this is using cURL; however, Solaris' native compiler has an issue with cURL versions before 7.49.0, so I can't connect via https which is a requirement, and I can't update cURL as I'm working on a company machine.
Every time I try to use wget to POST I get an ERROR: 400 Bad Request. It occurs to me that usually this is a problem with the json I'm trying to send but I can't for the life of me figure out what is wrong.
OS: Solaris 10
Version: wget 1.9.1
Command:
wget --post-file=data.json --http-user=[userID] --http-passwd=[userPass] --header="Content-Type: application/json" -i "uri.txt"
data.json:
{
"fields": {
"project":
{
"id": "10200"
},
"summary": "Creating issue remotely.",
"description": "Creating of an issue using project keys and issue type names using the REST API",
"issuetype": {
"name": "Story"
},
"customfield_11600": {
"id": "11303"
}
}
}
UPDATE:
I think my header is not correct when I send out my http POST. In my wget command above I set the Content-Type to application/json; however, looking at the debug information there is two Content-Type spaces in my header.
Content-Type: application/x-www-form-urlencoded
Content-Length: 175
Content-Type: application/json
Could it be that I'm setting my Content-Type incorrectly and the POST is being sent with the wrong encoding?
UPDATE 2
I think I've ruled out the above update as an issue, as removing the application/json content-type gives a 415 Unsupported Media Type error, implying that adding it makes the POST send the correct media type.
I've gotten a curl command to work properly on my windows machine and it connects via http1.1 automatically while wget connects via http1.0, I believe that when connecting via http1.0 the data being sent is appended on the end of the URI. Could this be why the server believes the json to be incorrect?

Issue adding attachments to documents in CouchDB

I'm having problems adding attachments in CouchDB. First off, when I ran the test suite in Futon I got two errors - one in attachments, and another in replication. The error specific to attachments isn't particularly helpful -
name
attachments
status
error
duration
6112
details
0
Exception raised: {}
Some internet commentary says that a few errors in the test suite is perfectly normal, so I moved on. However, attachments are definitely not working properly, either via Futon or Curl. In Futon, the file will generally attach, but only after I click cancel on the upload - i.e. it hangs on the upload progress bar indefinitely until I click cancel. I'm accessing futon on a remote machine via ssh -L5984:127.0.0.1:5984 root#myServer and then pointing Firefox at localhost:5984/_utils/. I don't know if that could be making a difference.
When I try to upload via Curl, using the command
curl -vX PUT http://127.0.0.1:5984/albums/6e1295ed6c29495e54cc05947f18c8af/ artwork.jpg?rev=9--43332bfae07884c683b50ffd4b8ee18c --data-binary #artwork.jpg -H "Content-Type: image/jpg"
I get:
Host: 127.0.0.1:5984
Accept: /
Content-Type: image/jpg
Content-Length: 11205
Expect: 100-continue
< HTTP/1.1 100 Continue
< HTTP/1.1 400 Bad Request
* HTTP error before end of send, stop sending
{"error":"bad_request","reason":"invalid UTF-8 JSON"}
The log file shows:
[debug] [<0.103.0>] 'PUT' /albums/6e1295ed6c29495e54cc05947f18c8af/ {1,1}
Headers: [{'Accept',"*/*"},
{'Content-Length',"11205"},
{'Content-Type',"image/jpg"},
{"Expect","100-continue"},
{'Host',"127.0.0.1:5984"},
{'User-Agent',"curl/7.22.0 (x86_64-pc-linux-gnu) libcurl/7.22.0 OpenSSL/1.0.1 zlib/1.2.3.4 libidn/1.23 librtmp/2.3"}]
[debug] [<0.103.0>] OAuth Params: []
[error] [<0.103.0>] attempted upload of invalid JSON (set log_level to debug to log it)
Followed by a huge block -
[debug] [<0.103.0>] Invalid JSON:
<<255,216,255,224,0,16,74,70,73,70,0,1,1,0,0,1,0,1,0,0,255,219,0,132, ... , etc
The final two lines in the log that are relevant to the attempted upload are
[info] [<0.103.0>] 127.0.0.1 - - 'PUT' /albums/6e1295ed6c29495e54cc05947f18c8af/ 400
[debug] [<0.103.0>] httpd 400 error response: {"error":"bad_request","reason":"invalid UTF-8 JSON"}
Does anyone have any idea what is going on here? I tried wrapping the string in the curl request around ' ' in case bash was eating the quotes, it made no difference.
There is a spurious space in your curl command after the slash between the doc id and the attachement:
...f18c8af/ artwork.jpg?...
^
Does it work when you remove it?
There were two problems. The first was the spurious space that Simon pointed out - tyvm. The other problem was where the rev-x details are placed. I was using the format in the book http://guide.couchdb.org/editions/1/en/api.html.
Original command:
curl -vX PUT http://127.0.0.1:5984/albums/6e1295ed6c29495e54cc05947f18c8af/ artwork.jpg?rev=9--43332bfae07884c683b50ffd4b8ee18c --data-binary #artwork.jpg -H "Content-Type: image/jpg"
Working command (space removed & rev placed in the correct order):
curl -vX PUT http://127.0.0.1:5984/albums/6e1295ed6c29495e54cc05947f18c8af/attachment?rev=9-43332bfae07884c683b50ffd4b8ee18c --data-binary #artwork.jpg -H "Content-Type: image/jpg"

how to post with curl to REST/JSON service?

I have tried out the sample REST JSON service at
http://www.javacodegeeks.com/2013/04/spring-mvc-easy-rest-based-json-services-with-responsebody.html
and the JQuery client can successfully post a person object to the service! Yahoo!
How do I do the same thing with cURL?
Here is my attempt:
curl -i -X POST -H "Content-Type: application/json; charset=UTF-8" -H "Accept: application/json" -d "{'name':'siegfried','age':26}" http://localhost:8080/api/person
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 71 100 42 100 29 39 26 0:00:01 0:00:01 --:--:-- 40
HTTP/1.1 200 OK
Content-Type: application/json;charset=UTF-8
Content-Length: 42
Server: Jetty(6.1.25)
Saved person: Person [name=null, age=null]
Compilation finished at Sun Dec 08 22:14:05
It is not parsing the data! How do I make it parse my json data?
I've been google searching for hours and trying out many combinations of escaping quotes and apostrophes and the like and nothing seems to work.
Thanks
siegfried
The -d will send your data as a post, you do not need to declare POST.
man curl gives the description on how to use -d. If anyone else would need it.
A simple Sinatra server http://www.sinatrarb.com, or something like it, can be used to debug your curls as well as a mock server
Is there a possibility that the problem is on the server-side, if you run it on local host you should have access to it, right?
I found the problem!
The spring #ResponseBody uses JSON for get but uses URL Encoding for POST.
See the jQuery POST method for http://codetutr.com/2013/04/09/spring-mvc-easy-rest-based-json-services-with-responsebody/
Why the inconsistency? This drove me nuts!
Well, I still don't have a cURL command. But I do have java and groovy clients that can now POST and GET. I should have a cURL soon.

curl get json returns 404

I'm running a tomcat server with a REST service on localhost.
This url typed in the browser gives me a json response: http://localhost:8080/a/rest/dataset/list
But when I use curl: curl http://localhost:8080/a/rest/dataset/list the response is HTML code with a 404 error from the server.
What am I missing in the curl command?
Tomcat may be waiting for a specially formatted HTTP request from the user. As you are expecting a response in json, you should probably forward the following argument to curl :
-H 'Content-Type: application/json'
Also, you may always specify to whatever HTTP server the response you are expecting him to return. A lot of back-end application based on HTTP are issuing a response given the HTTP request.