0

I recently posted a question regarding creating a document in a collection in the local emulator suite using a HTTP Post Request with Axios.

Creating new document with Firestore REST API and Local Emulator Suite, Returning Error 404: Problem with Path Parameter

My previous error was a 404 error with the URL path parameter. Since making changes to that, now I’m receiving a 400 error. The following is the code:

First, using a post request to create an authenticated user ID token and storing that in a variable. No issues here.

        //HTTP Post Request to create an auth ID, storing ID Token in variable 

    const createUserResponse = await axios.post(createUserInstance.url, createUserInstance.data, createUserInstance.config);
    const userIdToken = createUserResponse.data.idToken;
    const userLocalId = createUserResponse.data.localId;
    console.log(userIdToken);
    console.log(userLocalId);

Second, writing the request body. I’ve modified the URL which works now. I’ve also re-modified the data body to make sure it’s in the correct format. Wondering if the issue is in:

  • the way I’ve written the query parameters (do I need to specific the new document name? is the API key the problem?)

  • the formatting of the data request body : according to the documentation a new document is automatically generated. I’d presume fields would be read as containing the key-value pairs to include in the document fields.

  • the formatting of the headers. I’ve checked and re-checked this. My userIDToken contains a string

          //create a document in the user collection
    
      //request body 
      const createDocumentInstance : createPostRequest = {
          url: 'http://localhost:8080/v1beta1/projects/okane-crud-dev/databases/(default)/documents/test?key=<API_KEY>',
          data: {
              'fields': {
                  'localId': 'hello',
              }
          },
          //directly pasted IdToken as using the variable resulted in problem with ' ' error
          config: {
              'headers': 
                  {
                      'Content-Type': 'application/json',
                      'Authorization': `Bearer ${userIdToken}`,
                  }
      }};
    

To make sure of what I was looking at, I logged the entire request in my console. This is what it looks like.

   console.log
  {
    url: 'http://localhost:8080/v1beta1/projects/okane-crud-dev/databases/(default)/documents/test?key=AIzaSyCQSnirvajGL5Uok34OgEn7tF1S_tp5sa0',
    data: { fields: { localId: 'hello' } },
    config: {
      headers: {
        'Content-Type': 'application/json',
        Authorization: 'Bearer eyJhbGciOiJub25lIiwidHlwIjoiSldUIn0.eyJlbWFpbCI6Im15ZW1haWxAZW1haWwuY29tIiwiZW1haWxfdmVyaWZpZWQiOmZhbHNlLCJhdXRoX3RpbWUiOjE2NjU0NTQ2MDgsInVzZXJfaWQiOiI1Vmt3TUtRc1k0THJRTkRWaXpFYmdnYnExOVNyIiwiZmlyZWJhc2UiOnsiaWRlbnRpdGllcyI6eyJlbWFpbCI6WyJteWVtYWlsQGVtYWlsLmNvbSJdfSwic2lnbl9pbl9wcm92aWRlciI6InBhc3N3b3JkIn0sImlhdCI6MTY2NTQ1NDYwOCwiZXhwIjoxNjY1NDU4MjA4LCJhdWQiOiJva2FuZS1jcnVkLWRldiIsImlzcyI6Imh0dHBzOi8vc2VjdXJldG9rZW4uZ29vZ2xlLmNvbS9va2FuZS1jcnVkLWRldiIsInN1YiI6IjVWa3dNS1FzWTRMclFORFZpekViZ2dicTE5U3IifQ.'
      }
    }
  }

Finally, making the post request on Axios. Exact same syntax as my previous post request.

        //Post Request to create a document 
    const createDocument = await axios.post(createDocumentInstance.url, createDocumentInstance.data, createDocumentInstance.config); 
    const docReference = createDocument.data;
    console.log(docReference);

When running this, the following error is returned:

{
    message: 'Request failed with status code 400',
    name: 'Error',
    description: undefined,
    number: undefined,
    fileName: undefined,
    lineNumber: undefined,
    columnNumber: undefined,
    stack: 'Error: Request failed with status code 400\n' +
      '    at createError (/Users/georgettekoo/Documents/Code/Okane/Okane-Firebase-Backend-Deprecated/functions/node_modules/axios/lib/core/createError.js:16:15)\n' +
      '    at settle (/Users/georgettekoo/Documents/Code/Okane/Okane-Firebase-Backend-Deprecated/functions/node_modules/axios/lib/core/settle.js:17:12)\n' +
      '    at IncomingMessage.handleStreamEnd (/Users/georgettekoo/Documents/Code/Okane/Okane-Firebase-Backend-Deprecated/functions/node_modules/axios/lib/adapters/http.js:293:11)\n' +
      '    at IncomingMessage.emit (node:events:539:35)\n' +
      '    at endReadableNT (node:internal/streams/readable:1344:12)\n' +
      '    at processTicksAndRejections (node:internal/process/task_queues:82:21)',
    config: {
      transitional: {
        silentJSONParsing: true,
        forcedJSONParsing: true,
        clarifyTimeoutError: false
      },
      adapter: [Function: httpAdapter],
      transformRequest: [ [Function: transformRequest] ],
      transformResponse: [ [Function: transformResponse] ],
      timeout: 0,
      xsrfCookieName: 'XSRF-TOKEN',
      xsrfHeaderName: 'X-XSRF-TOKEN',
      maxContentLength: -1,
      maxBodyLength: -1,
      validateStatus: [Function: validateStatus],
      headers: {
        Accept: 'application/json, text/plain, */*',
        'Content-Type': 'application/json',
        Authorization: 'Bearer eyJhbGciOiJub25lIiwidHlwIjoiSldUIn0.eyJlbWFpbCI6Im15ZW1haWxAZW1haWwuY29tIiwiZW1haWxfdmVyaWZpZWQiOmZhbHNlLCJhdXRoX3RpbWUiOjE2NjU2MzAwMTUsInVzZXJfaWQiOiJEMTBoblpsek9nQWR0ZlJlNm1VUDBOY2ZtNm5pIiwiZmlyZWJhc2UiOnsiaWRlbnRpdGllcyI6eyJlbWFpbCI6WyJteWVtYWlsQGVtYWlsLmNvbSJdfSwic2lnbl9pbl9wcm92aWRlciI6InBhc3N3b3JkIn0sImlhdCI6MTY2NTYzMDAxNSwiZXhwIjoxNjY1NjMzNjE1LCJhdWQiOiJva2FuZS1jcnVkLWRldiIsImlzcyI6Imh0dHBzOi8vc2VjdXJldG9rZW4uZ29vZ2xlLmNvbS9va2FuZS1jcnVkLWRldiIsInN1YiI6IkQxMGhuWmx6T2dBZHRmUmU2bVVQME5jZm02bmkifQ.',
        'User-Agent': 'axios/0.24.0',
        'Content-Length': 30
      },
      method: 'post',
      url: 'http://localhost:8080/v1beta1/projects/okane-crud-dev/databases/(default)/documents/test?key=AIzaSyCQSnirvajGL5Uok34OgEn7tF1S_tp5sa0',
      data: '{"fields":{"localId":"hello"}}'
    },
    code: undefined,
    status: 400
  }

Not sure what I'm missing and where the issue is. Does anyone have any hints?

flythejet
  • 33
  • 3

0 Answers0