Select to view content in your preferred language

REST API Add Feature to Layer POST Request: tokens and content-types

1660
1
Jump to solution
07-17-2023 12:34 PM
KaterynaM
New Contributor II

Hi there,

I have been trying to make an addFeatures POST request to a FeatureServer hosted within my organization from a javascript webapp using the Javascript SDK. I run into no issues when making the POST request from the FeatureServer itself, as the request has content-type=application/x-www-form-urlencoded and there is a token included as a parameter in the url of the POST request. However, I am running into a few interconnected issues when attempting to do the same operation through a javascript application: 

Version 1: using esriRequest

 let feats = [
    {
        "geometry": <geometry>,  
        "attributes": <attributes>
    }
]
let params = `features=${JSON.stringify(feats)}`
let url = layerName.url + '/0/addFeatures'
const response = await esriRequest(url, {
    body: params,
    method: "post",
    responseType: "json"
})

Problem: the response that comes back is the html of the FeatureServer page used to make the POST request, and this does not actually make the POST request itself. Investigations show that the content-type of this request are plain/text, and no token is included in the url. Additionally, I'm unsure of whether the params should go in the body or the query of the requestParameters, since neither seem to work.

 

Version 2: using addFeatures 

let feats = [
   {
       geometry: <geometry>,  
       attributes: <attributes>
   } as IFeature
]
addFeatures({
   url: layerName.url + "/0/",
   features: feats,
}).then((response) => console.log("response", response))

 This solves the content-type problem, but there is still no token attached to the POST request, and I get an error about this in the response. It seems the token must be included as a parameter in the url or as an authentication property in the addFeatures object, but it is unclear where this token comes from, as the application uses OAuth2 for user authentication, not IdentityManagers that can call generateToken or esriConfig.ApiKeys.  Is the token the same as the local client app id, or generated on user login, or regenerated for each POST request made to the server?

0 Kudos
1 Solution

Accepted Solutions
KaterynaM
New Contributor II

Update, this has been resolved! And it may be of use to those using specifically the ArcGIS REST JS API.

The key element missing here was the fromCredential function from the REST JS API, described here, and the findCredential function from the Maps SDK for Javascript, described here. I ended up using the portal url in findCredential and taking the manager returned from fromCredential in the authentication property of the addFeatures options object. Hope this helps other folks who may be facing similar issues!

 

 

View solution in original post

0 Kudos
1 Reply
KaterynaM
New Contributor II

Update, this has been resolved! And it may be of use to those using specifically the ArcGIS REST JS API.

The key element missing here was the fromCredential function from the REST JS API, described here, and the findCredential function from the Maps SDK for Javascript, described here. I ended up using the portal url in findCredential and taking the manager returned from fromCredential in the authentication property of the addFeatures options object. Hope this helps other folks who may be facing similar issues!

 

 

0 Kudos