Issue with uploading multi-line Markdown files to Sanity API using Python's requests library.

12 replies
Last updated: Aug 8, 2020
Hey everyone. Hoping you can help me with something.
I am trying to upload Markdown files to Sanity using the Sanity API. I am submitting a POST request using Python's
requests
library. My API call returns an HTTP 400 whenever I try to submit multi-line Markdown files, which is a problem. Here is some sample code, if it helps:

headers = {

'Authorization': f'Bearer {SANITY_API_TOKEN}',

'Content-Type': 'application/json',

}


data = f'''{{"mutations":[

{{"createOrReplace": {{

"_id": "AAPL",

"_type": "dataFeed",

"slug": "aapl",

"postType": "stock",

"publishedAt": "2020-08-08",

"title": "Apple Inc. (AAPL)",

"body": "I want this to be a multi-line string, but it returns HTTP 400 when I submit one "

}}}}

]}}'''


response = <http://requests.post|requests.post>('https://{project ID}.<http://api.sanity.io/v1/data/mutate/production|api.sanity.io/v1/data/mutate/production>', headers=headers, data=data)
Aug 8, 2020, 2:48 PM
Hi there
user H
!
I just tested your request in Postman, and it seems like the json is not formatted correctly. Can you try this, it works for me in Postman at least:


{
	"mutations":[
    	{
    		"createOrReplace": {
        		"_id": "AAPL",
        		"_type": "dataFeed",
        		"slug": "aapl",
        		"postType": "stock",
        		"publishedAt": "2020-08-08",
        		"title": "Apple Inc. (AAPL)",
        		"body": "I want this to be a multi-line string, but it returns HTTP 400 when I submit one "
    		}
    	}
    ]
}
Aug 8, 2020, 4:56 PM
user J
that one returns a 400 response too. HEre's an example of the data (without interpolation) that returns a successful 200 response
Aug 8, 2020, 8:15 PM
{"mutations":[

{"createOrReplace": {

"_id": "FB",

"_type": "dataFeed",

"slug": "FB",

"postType": "stock",

"publishedAt": "2020-08-08",

"title": "Facebook, Inc. (FB)",

"body": "Multi line string location"

}}

]}
Aug 8, 2020, 8:15 PM
I just need to figuer out how to replace "multi line string location" with a multi-line string
Aug 8, 2020, 8:15 PM
are you encoding your new line characters? like
\n
?
Aug 8, 2020, 8:31 PM
when I add an
\n
it returns an HTTP 400 response
Aug 8, 2020, 8:33 PM
Here's the sample
Aug 8, 2020, 8:33 PM
{"mutations":[

{"createOrReplace": {

"_id": "FB",

"_type": "dataFeed",

"slug": "FB",

"postType": "stock",

"publishedAt": "2020-08-08",

"title": "Facebook, Inc. (FB)",

"body": "Multi line

string location"

}}

]}
Aug 8, 2020, 8:33 PM
the string needs to contain the
\n
like ``Multi line \n string location.` You can't have actual new lines in a string, so you need to encode those to a
\n
character.
Aug 8, 2020, 8:35 PM
this is my full mutation that works:

{
	"mutations":[
    	{
    		"createOrReplace": {
            	"_id": "FB",
	        	"_type": "dataFeed",
            	"slug": "FB",
            	"postType": "stock",
            	"publishedAt": "2020-08-08",
            	"title": "Facebook, Inc. (FB)",
            	"body": "Multi line \n string location"
        	}
        }
    ]
}
Aug 8, 2020, 8:42 PM
I got it to work! Thank you SO much!
Aug 8, 2020, 8:49 PM
Kool kool kool! Happy to help!
Aug 8, 2020, 8:51 PM

Sanity– build remarkable experiences at scale

The Sanity Composable Content Cloud is the headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.

Was this answer helpful?

Categorized in