MS Flow HTTP POST to update Feature Service??

1737
7
Jump to solution
06-24-2019 07:00 PM
PNWGuy
by
Occasional Contributor

I have set up a Microsoft flow to trigger when rows in my Smartsheet are updated. I followed this tutorial on how to update values using the ArcGIS REST API. Everything works as it should using Postman. I noticed however that the ArcGIS Rest API is expecting multipart/form-data including key value pairs for f:json, a valid token, and then the updates. Postman handles this natively by selecting the form/data option for the body and automatically creates boundaries. MS flow apparently does not handle multipart/form-data natively. How do I structure my body to pass a valid POST request to the API to update my feature service? Screenshot of MS Flow HTTP Post below. Also below is the successful POST from Postman using the exact same data. I should note that this feature service is shared with editing enabled, which explains my missing token key value pair.

From Postman Console. This successfully posts:

Request Header:
User-Agent:"PostmanRuntime/7.15.0"
Accept:"*/*"
Cache-Control:"no-cache"
Postman-Token:"8988b891-ad8f-4e9f-8be0-965ad2711ecc"
Host:"services.arcgis.com"
accept-encoding:"gzip, deflate"
content-type:"multipart/form-data; boundary=--------------------------113767685070943500397041"
content-length:467
Connection:"keep-alive"
Request Body:
f:"json"
updates:"[ { "attributes" : { "objectId": 3, "MissionDate" : "2019-06-27T07:00:00.0000000", "MissionName" : "Waste Transfer Facility Promo Shots", "Status" : "pending_review" } } ]"‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
1 Solution

Accepted Solutions
PNWGuy
by
Occasional Contributor

All, I finally got this working and I want to share the solution and some things I have learned about MS Flow and testing the ArcGIS REST API using Postman. Maybe you seasoned programmers know this stuff, but I sure didn't. Ultimately, and most importantly it is possible to use the MS Flow HTTP trigger with the ArcGIS REST API to edit features, but it is a little tricky. The most annoying thing I found (and I'm not sure if MS Flow is at fault or the ArcGIS REST API) is almost always the response from the API will be 200 (Success), even though it is not successful. Below is a malformed request, but you can see we get a 200 back. It should respond back as a 400:

For awhile, I thought that my Flow was working properly, until I realized my features were not updating. Lesson learned, do not trust or rely upon feedback from MS flow. This little green box is deceptive.

I also learned some valuable info about Postman. I assumed that pasting in the raw sent request from the Postman Console into the body of the Flow HTTP trigger would work. (Because hey, the request works great in Postman - everything updated as it should). But even in "raw" mode, the console doesn't actually show the true request that was sent to the API. However, the "Generate Code Snippet" tool in HTTP mode does. This ultimately helped me figure out what to paste into the header and body portion of Flow.

Ultimately, pictured below is how you would format your header and body (configure your token for authentication under the "Show advanced options" at the bottom) to successfully POST to a feature using the ArcGIS REST API to update attribute features. The main trick is defining your boundary (unique string of characters which separates your different forms in the body) in the header (in my case I just used what Postman auto-generated, but it could be anything). I hope this saves you some time if you want to use MS Flow and the ArcGIS Rest API. 

View solution in original post

7 Replies
PNWGuy
by
Occasional Contributor

All, I finally got this working and I want to share the solution and some things I have learned about MS Flow and testing the ArcGIS REST API using Postman. Maybe you seasoned programmers know this stuff, but I sure didn't. Ultimately, and most importantly it is possible to use the MS Flow HTTP trigger with the ArcGIS REST API to edit features, but it is a little tricky. The most annoying thing I found (and I'm not sure if MS Flow is at fault or the ArcGIS REST API) is almost always the response from the API will be 200 (Success), even though it is not successful. Below is a malformed request, but you can see we get a 200 back. It should respond back as a 400:

For awhile, I thought that my Flow was working properly, until I realized my features were not updating. Lesson learned, do not trust or rely upon feedback from MS flow. This little green box is deceptive.

I also learned some valuable info about Postman. I assumed that pasting in the raw sent request from the Postman Console into the body of the Flow HTTP trigger would work. (Because hey, the request works great in Postman - everything updated as it should). But even in "raw" mode, the console doesn't actually show the true request that was sent to the API. However, the "Generate Code Snippet" tool in HTTP mode does. This ultimately helped me figure out what to paste into the header and body portion of Flow.

Ultimately, pictured below is how you would format your header and body (configure your token for authentication under the "Show advanced options" at the bottom) to successfully POST to a feature using the ArcGIS REST API to update attribute features. The main trick is defining your boundary (unique string of characters which separates your different forms in the body) in the header (in my case I just used what Postman auto-generated, but it could be anything). I hope this saves you some time if you want to use MS Flow and the ArcGIS Rest API. 

View solution in original post

PaulSweeney3
Occasional Contributor II

Great post this looks really useful and i was looking to do something similar. i have set up similar flows previously looking to edit a feature service following the submission of a survey123  form ,which were successful however i now need to do something similar and add new information based on rows added in smartsheet.  i can understand your HTTP request however what i cant grasp is how you passed the values from smart sheet.  How did you manage to specify the specific colume from the row added ? 

0 Kudos
PNWGuy
by
Occasional Contributor

Hi Paul Sweeney,

That requires a little work with the smartsheets api first. Use the Get Row api for one of your smartsheets records. The response will contain an array of your data and it will identify the "columnID". Document the columnIDs that you will want to push back to AGO. 

You will have to figure out how you want to trigger your flow when a new smartsheet row is added. You can create a webhook in Smartsheets than can send a response to your flow, however I didn't really have the patience for this. It's probably cleaner, but will take longer to properly set up. I found it easier to just have Smartsheets send a email to my outlook account (making sure the email contained my feature GUID [make sure you pass the GUID and ObjectID of you feature from Survey123/GIS into smartsheets]). My flow triggers when I receive this email.

Flow parses through the text body of the email to find the GUID. Flow then searches for the smartsheet row with this GUID (the row I was just notified about) using the search api. Then create an array variable that you can store your smartsheet data in. Next, loop (apply to each) through the json items, testing each (use a switch) with the ColumnIds of interest and store in your array. Be sure you pay attention to the order of your data in your array. You then create single variables of your data by composing expressions specifying the data location in the array [0,1,2,3,etc]. Now each data you extracted from the array is segmented individually and you can then plop them into the HTTP request to the ArcGIS REST api. Hope this is helpful.

0 Kudos
FranciscoRodriguez2
New Contributor II

Such a helpful post, learned how to looks at how Postman structures the request.  I was wondering if your were still having success with this method.  I has success posting using Postman but when I try to structure the POST request using the code from Postman, I am getting the 200 code response but the data is not being pushed to the hosted feature layer.  I have shared the layer publicly in order to avoid dealing with the token for now.   Any ideas? Or suggestion? Would be deeply grateful!

 addfeature.PNG

0 Kudos
PNWGuy
by
Occasional Contributor

Hi @FranciscoRodriguez2,

Sorry for the delay. I still use MS Flow (Power Automate now) a lot and create and update features, though I no longer use Smartsheets. We use Monday.com now. And this method still works. Your screenshot is really hard for me to make out. One trick is to open up your console and look at exactly what postman is sending to the API. This might help you shake it lose.

PavelPelikán
New Contributor

Hi, great!
Thanks for this article, is there any chance how to manage the flow with original XML body? Or any option how to transfer xml body to "HTTP code snippet" automatically? 

Thank you a lot.

Pavel

0 Kudos
PNWGuy
by
Occasional Contributor

Hi Pavel,

Not sure I am quite following. Are you asking how to export the Flow as a XML, then have Postman run it?

0 Kudos