title: Problem sending POST body to the Firestore REST API
body: <p>I want to create a new document in Firestore using the REST API.</p>
  <p>Very good examples here using Axios to send the POST request with some fields:<br>
  <a href="https://www.jeansnyman.com/posts/google-firestore-rest-api-examples/" rel="nofollow noreferrer">https://www.jeansnyman.com/posts/google-firestore-rest-api-examples/</a></p>
  <pre><code>axios.post(
  &quot;https://firestore.googleapis.com/v1/projects/&lt;PROJECTIDHERE&gt;/databases/(default)/documents/&lt;COLLECTIONNAME&gt;&quot;, 
      { 
          fields: { 
              title: { stringValue: this.title }, 
              category: { stringValue: this.category }, 
              post: { stringValue: this.post }, 
              summary: { stringValue: this.description }, 
              published: { booleanValue: this.published }, 
              created: { timestampValue: new Date() }, 
              modified: { timestampValue: new Date() } 
          } 
      }
      ).then(res =&gt; { console.log(&quot;Post created&quot;) })
  </code></pre>
  <p>And an example here using Python Requests:<br>
  <a href="https://stackoverflow.com/questions/59076188/using-the-firestore-rest-api-to-update-a-document-field">Using the Firestore REST API to Update a Document Field</a><br>
  (this is a PATCH request but the field formatting is the same as in a POST request)</p>
  <pre><code>import requests
  import json
  
  endpoint = &quot;https://firestore.googleapis.com/v1/projects/[PROJECT_ID]/databases/(default)/documents/[COLLECTION]/[DOCUMENT_ID]?currentDocument.exists=true&amp;updateMask.fieldPaths=[FIELD_1]&quot;
  
  body = {
      &quot;fields&quot; : {
          &quot;[FIELD_1]&quot; : {
              &quot;stringValue&quot; : &quot;random new value&quot;
          }
      }
  }
  
  data = json.dumps(body)
  
  headers = {&quot;Authorization&quot;: &quot;Bearer [AUTH_TOKEN]&quot;}
  print(requests.patch(endpoint, data=data,  headers=headers).json())
  </code></pre>
  <p>I am using Google Apps Script UrlFetchApp.fetch to send my requests.  I am able to use GET requests with no problems.  For example, to get all the documents in a collection (in Google Apps Script):</p>
  <pre><code>function firestore_get_documents(){
    var options = {
      headers: { 'Authorization': 'Bearer ' +  ScriptApp.getOAuthToken() },
      method:'GET'
    }
    var response = UrlFetchApp.fetch('https://firestore.googleapis.com/v1/projects/&lt;PROJECTIDHERE&gt;/databases/(default)/documents/myCollection', options);
    var parsed = JSON.parse(response.getContentText());
    return parsed;
  }
  </code></pre>
  <p>This works nicely.  And changing 'method' to 'POST' creates a new document in myCollection as expected.  Then I try to add a POST body with some fields (or just one field):</p>
  <pre><code>function firestore_create_new_document(){
    
    var options = {
      headers: { 'Authorization': 'Bearer ' +  ScriptApp.getOAuthToken() },
      method:'POST',
      payload: {fields: { title: { stringValue: 'newTitle' } } }, // If you comment out this line, it works as expected
      muteHttpExceptions:true
    }
    var response = UrlFetchApp.fetch('https://firestore.googleapis.com/v1/projects/&lt;PROJECTIDHERE&gt;/databases/(default)/documents/myCollection', options);
    var contentText = response.getContentText();
    var parsed = JSON.parse(response.getContentText());
    return parsed;
  }
  </code></pre>
  <p>I get the following errors:</p>
  <pre><code>code: 400 message: &quot;Request contains an invalid argument.&quot;
  status: &quot;INVALID_ARGUMENT&quot;
  details[0][@type]: &quot;type.googleapis.com/google.rpc.BadRequest&quot;
  details[0][fieldViolations][0][field]: &quot;{title={stringValue=newTitle}}&quot;
  details[0][fieldViolations][0][description]: &quot;Error expanding 'fields' parameter. Cannot find matching fields for path '{title={stringValue=newTitle}}'.&quot;
  </code></pre>
  <p>Documentation is available here:<br>
  <a href="https://firebase.google.com/docs/firestore/reference/rest/v1/projects.databases.documents/createDocument" rel="nofollow noreferrer">https://firebase.google.com/docs/firestore/reference/rest/v1/projects.databases.documents/createDocument</a>
  <a href="https://firebase.google.com/docs/firestore/reference/rest/v1/projects.databases.documents#Document" rel="nofollow noreferrer">https://firebase.google.com/docs/firestore/reference/rest/v1/projects.databases.documents#Document</a></p>
  <ol>
  <li>The problem may be the formatting of my 'fields' object - I've tried several different formats from the documentation and examples</li>
  <li>The problem may be that the fields don't exist yet?  I think I should be able to create a new document with new fields</li>
  <li>The problem may be with the way UrlFetchApp.fetch sends my JSON body.  I have tried using payload = JSON.stringify(payload_object) and that doesn't work either.</li>
  </ol>
  <p>I think UrlFetchApp is doing something slightly different than Axios or Python Requests - the body is getting sent differently, and not parsing as expected.</p>