Issue with uploading multi-line Markdown files to Sanity using the API
12 replies
Last updated: Aug 8, 2020
H
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
I am trying to upload Markdown files to Sanity using the Sanity API. I am submitting a POST request using Python's
requestslibrary. 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
J
Hi there
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:
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
H
user J
that one returns a 400 response too. HEre's an example of the data (without interpolation) that returns a successful 200 responseAug 8, 2020, 8:15 PM
H
{"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
H
I just need to figuer out how to replace "multi line string location" with a multi-line string
Aug 8, 2020, 8:15 PM
J
are you encoding your new line characters? like
\n?
Aug 8, 2020, 8:31 PM
H
when I add an
\nit returns an HTTP 400 response
Aug 8, 2020, 8:33 PM
H
Here's the sample
Aug 8, 2020, 8:33 PM
H
{"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
J
the string needs to contain the
\nlike ``Multi line \n string location.` You can't have actual new lines in a string, so you need to encode those to a
\ncharacter.
Aug 8, 2020, 8:35 PM
J
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
H
I got it to work! Thank you SO much!
Aug 8, 2020, 8:49 PM
J
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.