Zum Hauptinhalt gehen

How to set Content-Type in a webrequest

Kommentare

17 Kommentare

  • Ryan Cooney

    Hi Sebastian,

    (https://data.latitudegeo.com/community/WorkflowSamples/webrequestwithheaders.xaml) This workflow provides an example of assigning headers to a WebRequest activity.

    It is a little awkward because you need to use the InvokeMethod activity to do this but it is possible. Have a look at the Parameters collection of the two InvokeMethod activities. You can use Fiddler to verify that the assigned headers are sent.

    --Ryan

    0
  • Hi Ryan

    Your example worked fine, but if I try to set the Content-Type to "application/json" in a POST request i got this error

    "The Content-Type header cannot be changed from its default value for this request."

     

     

     

    0
  • Ryan Cooney

    Hi Sebastian,

    I can see the issue you are having now. I'm not sure if there is a way to get the Web Request activity to do exactly what you want here. This activity takes a list of parameters that are name/value pairs and includes them as the data for the request. By making a web request with name/value pairs we are basically requiring that the content type is "application/x-www-form-urlencoded". In your case it sounds like you need to POST a JSON string to the URL. This activity doesn't provide a way for you to supply the entire payload for the request.

    --Ryan

    0
  • Ryan Cooney

    Hi Sebastian,

    I've cooked up a C# Script activity that does this: (https://data.latitudegeo.com/community/WorkflowSamples/webrequest_addfeatures.xaml) w (https://data.latitudegeo.com/community/WorkflowSamples/webrequestwithheaders2.xaml) ebrequestwithheaders2.xaml

    This isn't the first time we've encountered the need to make a "raw" web request. I'll file a new feature request to cover this.

    --Ryan

    0
  • Thanks. It's seem to do the trick./customer/servlet/servlet.FileDownload?file=00P6000000e87wBEAQ

     

    0
  • Sebastian Mariendal Kristensen

    Hi Geoff

    You can see here how serialize objects to JSON

    http://weblogs.asp.net/hajan/archive/2010/07/23/javascriptserializer-dictionary-to-json-serialization-and-deserialization.aspx

    Then convert the json string to byte array 

    Encoding.UTF8.GetBytes(JSON)

    then use the C# script providet by Ryan and that should be it :-)

     

    0
  • Ryan Cooney

    Hi Geoff,

    The workflow you included isn't far off. The problem is that the format of the "features" parameter of the Web Request activity was incorrect. See this working example that submits an "add features" request to our demo server (https://data.latitudegeo.com/community/WorkflowSamples/webrequest_addfeatures.xaml) webrequest_addfeatures.xaml . You can run this completely in the workflow simulator.

    Since the ArcGIS Server FeatureLayer/addFeatures endpoint accepts URL/Form parameters there is no need to set the content type of the Web Request activity.

    Also, this very same task can be performed with the "Add Features" activity. It allows you to provide the features to add as a FeatureSet object, rather than manually assembling the JSON for them.

    --Ryan

    0
  • Ryan Cooney

    @Sebastian,

    As of Essentials 3.15 the Web Request activity supports the ability to make a raw web requests with arbitrary data and content-types. I've rewritten the previous C# Script example to do this (https://data.latitudegeo.com/community/WorkflowSamples/ webrequestwithheaders3.xaml) webrequestwithheaders3.xaml . You'll see that the "Raw Data" and "Headers" arguments of the Web Request activity now get assigned.

    --Ryan

    0
  • Permanently deleted user

    I've used this information to do some "ApplyEdits" to a table too using GC 3.14. You just have to write the right formatting using the "updates" parameter i.e.: "[{""attributes"":{""OBJECTID"":" + RelatedRowNumberStr + ",""" + FieldName2EditStr + """:""" + NewTextStr + """}}]". The Uri has to look like ..../featureServer/1/applyEdits and f has to be "json" or "pjson"

     

     

    0
  • Charlie Cheng
    @Ryan,

     

     I am new to web request, Could you please let me know how to download your webrequestwithheaders3.xaml? I have the same request to POST Json and get a response Json 

     

    thanks

     

    -Charlie

     

    --
    0
  • Permanently deleted user
    Hello

     

    I'm trying to do this too - and whilst from the above thread it looks like it's completely do-able I can't access any of the referenced workflows in the thread to see how.

     

    So basically I have a workflow which generates a JSON string; this utlimately needs to be passed to an external REST web service (POST) which accepts JSON in the message payload (Content-Type:application/json).

     

    So in my workflow I have:

     

    1) Converted my JSON string to byte data with (getBytesFromText with UTF8 encoding) - variable named 'outputRequestByte')

     

    2) In my webRequest activity I have then set the then set 'Raw Data' to this 'outputRequestByte' variable and assigned the URI endpoint.; and explicitly set the method to POST

     

    However in the webRequest  I think I also need to explcitly set the Content-Type header name to a value of "application/json", but whilst I have no problem setting the webRequest Header parameter to a variable of System.Net.WebHeaderCollection I am struggling to assign a name/value pair of "Content-Type:application/json" to this collection variable.

     

    Can anyone help - unfortunately the sample WebRequest workflow doesn't seem to go to this level of conbfiguration and I can't get to any of the workflows referenced in the above thread comments.....

     

    All help greatly apopreciated!

     

     
    0
  • Ryan Cooney
    Hi Phil,

     

    You can use the Assign activity to set the header on your WebHeaderCollection. Something like: To: webHeaderCollection1("Content-Type")

     

    Value: "application/json"

     

    --Ryan
    0
  • Permanently deleted user
    I've rehosted Ryan's workflows and corrected the links.
    0
  • Permanently deleted user
    Hi Phil,

     

    I have replied to your question in another thread with the working example.

     

    Regards,

     

    Mohammad Ashraf
    0
  • Permanently deleted user
    Thanks all - got this working...

     

    So in summary I did the following:

     

    1) Created a variable called 'webReqHeader' of type System.Net.WebHeaderCollection  

     

    2) Instantiated the object with an assign activtiy e.g. webReqHeader = new webHeaderCollection()

     

    3) Used a second assign activity to set the content-type  e.g. webReqHeader("Content-Type") = "application/json"

     

    Then passed the webHeaderReq to the webRequest activity along with the raw JSON data (serialised as UTF-8 byte data).

     

     

     
    0
  • Permanently deleted user
    Ryan, FYI, the link to webrequestwithheaders3.xaml above has a space just before the workflow name. The link returns a 404. Also, webrequestwithheaders2.xaml and webrequestwithheaders3.xaml do not work as written. The site used returns error: (401) Unauthorized.
    0
  • Permanently deleted user
    Ryan,

     

    I left out that webrequestwithheaders.xaml doesn't work as written, either. It returns "The remote name could not be resolved: 'sampleserver1.geocortex.com'"
    0

Bitte melden Sie sich an, um einen Kommentar zu hinterlassen.