Python REST API Status 201 Create - IIS Adds HTML To JSON Response [Answered]RSS

10 replies

Last post Nov 17, 2016 06:55 AM by gtscdsi

  • Python REST API Status 201 Create - IIS Adds HTML To JSON Response

    Nov 03, 2016 02:05 PM|roemhildtg|LINK

    I'm using IIS with wfastcgi to serve up a flask python application. Things were going well up until the point where I tried to create new data. The JSON Rest server I'm using sends a 201 - Created Status with the location of the new item along with the object in JSON. For some reason IIS is injecting some html in the front of this response, so my JSON client web browser has an error when trying to parse the response.

    Is it possible to disable this behavior in IIS where the html is injected? I have seen other answers to this and people mention "Switch to apache" or have "written an iis module". Neither of these seem like a good solution since I don't know how to write/install iis modules, and using apache imo defeats the purpose of using iis.

    The problem is similar to what is mentioned here:

    http://stackoverflow.com/questions/12074730/w7-pro-iis-7-5-overwrites-php-location-header-solved

    http://stackoverflow.com/questions/10594225/prevent-iis-from-changing-response-when-location-header-is-present?noredirect=1&lq=1

  • Re: Python REST API Status 201 Create - IIS Adds HTML To JSON Response

    Nov 04, 2016 02:26 AM|Jean Sun|LINK

    Hi roemhildtg,

    Sorry about that, it seems that this bug is not fixed yet.

    roemhildtg

    The JSON Rest server I'm using sends a 201 - Created Status with the location of the new item along with the object in JSON. For some reason IIS is injecting some html in the front of this response, so my JSON client web browser has an error when trying to parse the response.

    Not sure what changed in your response header? Could you please provide some example?

    Based on my understanding, a workaround here is to modify the response header after the fastcgi model get executed.

    1. Write  a custom module, you can find how to Creating and Registering a Custom HTTP Module in the following link.

    https://msdn.microsoft.com/en-us/library/ms227673.aspx

    Similar issue about PHP and fastcgi : http://stackoverflow.com/questions/12074730/w7-pro-iis-7-5-overwrites-php-location-header-solved

    2. Use IIS URL Rewrite outbound rule to modify the response header

    Please find how to create outbound rule in the following link.

    https://blogs.msdn.microsoft.com/benjaminperkins/2012/11/02/change-or-modify-a-response-header-value-using-url-rewrite/

    Best Regards,

    Jean

    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue.
    If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
  • Re: Python REST API Status 201 Create - IIS Adds HTML To JSON Response

    Nov 04, 2016 01:17 PM|roemhildtg|LINK

    Hi Jean,

    Thank you for the response, this issue is driving me crazy :)

    Here's the request information:

    Request URL:http://greggtest.cityhall.com/api/workorder
    Request Method:POST
    Status Code:201 CREATED
    Remote Address:192.168.10.102:80
    Response Headers
    view source
    Access-Control-Allow-Origin:http://greggtest.cityhall.com
    Content-Length:1295
    Content-Type:text/html; charset=UTF-8
    Date:Fri, 04 Nov 2016 13:11:04 GMT
    Location:http://greggtest.cityhall.com/api/workorder/80
    Server:Microsoft-IIS/7.5
    Vary:Origin
    X-Powered-By:ASP.NET

    Here's the response body that IIS is injecting its html into:

    <head><title>Document Moved</title></head>
    <body><h1>Object Moved</h1>This document may be found <a HREF="http://greggtest.cityhall.com/api/workorder/80">here</a></body>{"data": {"attributes": {"created_by": null, "date_completed": null, "date_created": "2016-11-04T08:11:03.680000", "date_modified": "2016-11-04T08:11:03.680000", "details": "What", "is_complete": "Yes", "location": "Test", "modified_by": null}, "id": "80", "links": {"self": "http://greggtest.cityhall.com/api/workorder/80"}, "relationships": {"comments": {"data": [], "links": {"related": "/api/workorder?relation_name=comments&resource_id=80", "self": "/api/workorder/80/relationships/comments"}}, "crew": {"data": {"id": "1", "type": "workorder_crew"}, "links": {"related": "/api/workorder?relation_name=crew&resource_id=80", "self": "/api/workorder/80/relationships/crew"}}, "features": {"data": [], "links": {"related": "/api/workorder?relation_name=features&resource_id=80", "self": "/api/workorder/80/relationships/features"}}, "status": {"data": {"id": "1", "type": "workorder_status"}, "links": {"related": "/api/workorder?relation_name=status&resource_id=80", "self": "/api/workorder/80/relationships/status"}}, "task": {"data": {"id": "1", "type": "workorder_task"}, "links": {"related": "/api/workorder?relation_na.................

    The response body should be plain json. The web api returns Content-Type: application/vnd.api+json but as you can tell IIS is modifying the response by adding the document moved html and changing the content type to text/html

    I appreciate the links you posted but I don't really see how they are going to help me. I'm not sure where I would start writing a custom module to handle this. Also, I don't know if URL Rewrite is going to help, what sort of rule can I use to remove that html content from the json response?

    Edit:

    This is what the response looks like from my rest server:

    Content-Length →1290
    Content-Type →application/vnd.api+json
    Date →Fri, 04 Nov 2016 13:28:56 GMT
    Location →http://greggtest.cityhall.com:5000/api/workorder/81
    Server →Werkzeug/0.11.11 Python/2.7.12
    Vary →Origin

    {
      "data": {
        "attributes": {
          "created_by": null,
          "date_completed": "2016-11-02T20:07:23.108000+00:00",
          "date_created": "2016-11-02T13:59:46.873000+00:00",
          "date_modified": "2016-11-02T14:55:23.310000+00:00",
          "details": null,
          "is_complete": "Yes",
          "location": "lkj lkjl kjl kjlkjlj",
          "modified_by": null
        },
        "id": "81",
        "links": {
          "self": "http://greggtest.cityhall.com:5000/api/workorder/81"
        },
        "relationships": {
          "comments": {
            "data": [],
            "links": {
              "related": "/api/workorder?relation_name=comments&resource_id=81",
              "self": "/api/workorder/81/relationships/comments"
            }
          },
          "crew": {
            "data": null,
            "links": {
              "related": "/api/workorder?relation_name=crew&resource_id=81",
              "self": "/api/workorder/81/relationships/crew"
            }
          },
          "features": {
            "data": [],
            "links": {
              "related": "/api/workorder?relation_name=features&resource_id=81",
              "self": "/api/workorder/81/relationships/features"
            }
          },
          "status": {
            "data": {
              "id": "2",
              "type": "workorder_status"
            },
            "links": {
              "related": "/api/workorder?relation_name=status&resource_id=81",
              "self": "/api/workorder/81/relationships/status"
            }
          },
          "task": {
            "data": null,
            "links": {
              "related": "/api/workorder?relation_name=task&resource_id=81",
              "self": "/api/workorder/81/relationships/task"
            }
          }
        },
        "type": "workorder"
      },
      "included": [],
      "jsonapi": {
        "version": "1.0"
      },
      "links": {},
      "meta": {}
    }

  • Re: Python REST API Status 201 Create - IIS Adds HTML To JSON Response

    Nov 04, 2016 03:28 PM|Chaoix|LINK

    Do you get an Object Moved response every time or only sometimes?

    Object Moved is a real world scenario that you application should handle. It basically means the connection to the server was interrupted at some point by restarting IIS or the network kernel in windows, etc.

  • Re: Python REST API Status 201 Create - IIS Adds HTML To JSON Response

    Nov 04, 2016 05:34 PM|roemhildtg|LINK

    I only get the Object Moved response when I CREATE resources. It has to do with the JSON API specification.

    http://jsonapi.org/format/#crud-creating-responses

    http://jsonapi.org/format/#crud-creating-responses-201

  • Re: Python REST API Status 201 Create - IIS Adds HTML To JSON Response

    Nov 07, 2016 08:39 PM|roemhildtg|LINK

    I may be wrong but from my perspective, this situation is a common scenario:

     - Object submitted to API server

     - Object is saved to database and a ObjectID is given to the object by the database

     - API server returns a result to the client with the ObjectID and gives it a new "location" since the object is not actually found at /api/object/add, its found at /api/object/{ID}

    IIS is adding HTML to the response with this location, and it BREAKS ANY javascript app that would serialize this json.

    Is there really no workaround to have IIS not put the html header on top of my JSON response? I really don't understand why that would be considered "useful" and "required" by microsoft, without a simple way to override the behavior.

  • Re: Python REST API Status 201 Create - IIS Adds HTML To JSON Response

    Nov 08, 2016 04:11 PM|roemhildtg|LINK

    Hi Jean,

    Can you please provide more details on how I can write and implement a custom module to solve this issue?

    Or alternatively can you explain how I can modify the response header to fix the issue? I'm not super familiar with either solution and I don't want to break anything.

    I just want to disable the output of that junk html that IIS is inserting, is there no simple way to accomplish that?

  • Re: Python REST API Status 201 Create - IIS Adds HTML To JSON Response

    Nov 11, 2016 01:17 AM|Jean Sun|LINK

    Hi roemhildtg,

    I'm trying to involve some senior engineers into this issue and it will take some time. Your patience will be greatly appreciated.

    Sorry for any inconvenience and have a nice day!

    Best Regards,

    Jean

    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue.
    If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
  • Re: Python REST API Status 201 Create - IIS Adds HTML To JSON Response

    Nov 14, 2016 08:09 AM|gtscdsi|LINK

    Hi roemhildtg,

    Could you check if configure the MIME type and Handler mapping for JSON in IIS first? If not, you may try the following steps:

    • Open IIS Manager
    • Display properties for the IIS Server
    • Click MIME Types and then add the JSON extension:
      • File name extension: .json
      • MIME type: application/json
    • Go back to the properties for IIS Server
    • Click on Handler Mappings
      • Add a script map
      • Request path: *.json
      • Executable: C:\WINDOWS\system32\inetsrv\asp.dll
      • Name: JSON

    And then you can test again and see if the Content-Type correct this time.

    Hope it can help you.

    We focus on various troubleshooting plan and solution on IIS web platform and distributed applications. Please contact us at:

    http://blogs.msdn.com/b/asiatech/
  • Re: Python REST API Status 201 Create - IIS Adds HTML To JSON Response

    Nov 14, 2016 03:55 PM|roemhildtg|LINK

    Hello gtscdsi, thanks for the suggestions.

    I have added the mime type to the list of types in the server. It is set as follows:

    • Extension: .json
    • MIME Type: application/json
    • Entry Type: local

    I also set the Handler mapping:

    • Name: JSON
    • Path: *.json
    • State: Enabled
    • Path Type: Unspecified
    • Handler: IsapiModule
    • Entry Type: Local

    The server is still returning this response:

    Headers:

    Access-Control-Allow-Origin:http://greggtest.cityhall.com
    Content-Length:1058
    Content-Type:text/html; charset=UTF-8
    Date:Mon, 14 Nov 2016 15:50:58 GMT
    Location:http://greggtest.cityhall.com/api/workorder_feature/3949
    Server:Microsoft-IIS/7.5
    Set-Cookie:session=3dc38dd0-99d0-4044-9a21-aa6fca59d8e3; HttpOnly; Path=/
    Vary:Origin
    X-Powered-By:ASP.NET

    <head><title>Document Moved</title></head>

    <body><h1>Object Moved</h1>This document may be found <a HREF="http://greggtest.cityhall.com/api/workorder_feature/3949">here</a></body>{"data": {"attributes": {"completed_by": null, "created_by": "groemhildt", "date_created": "2016-11-14T09:51:00.370000", "date_modified": "2016-11-14T09:51:00.370000", "feature_id": 2002, "modified_by": "groemhildt", "note": ""}, "id": "3949", "links": {"self": "http://greggtest.cityhall.com/api/workorder_feature/3949"}, "relationships": {"layer": {"data": {"id": "7", "type": "geo_layer"}, "links": {"related": "/api/workorder_feature?relation_name=layer&resource_id=3949", "self": "/api/workorder_feature/3949/relationships/layer"}}, "status": {"data": {"id": "1", "type": "workorder_status"}, "links": {"related": "/api/workorder_feature?relation_name=status&resource_id=3949", "self": "/api/workorder_feature/3949/relationships/status"}}, "workorder": {"data": {"id": "115", "type": "workorder"}, "links": {"related": "/api/workorder_feature?relation_name=workorder&resourc

    You will also notice, the response body is not complete. It is getting trimmed off at 1058 characters...not sure if that is relevant to solving the issue but I found it interesting.

  • Re: Python REST API Status 201 Create - IIS Adds HTML To JSON Response

    Nov 17, 2016 06:55 AM|gtscdsi|LINK

    Hi roemhildtg,

    Thanks for your efforts. Then I am afraid it still need to write a custom module per http://stackoverflow.com/questions/12074730/w7-pro-iis-7-5-overwrites-php-location-header-solved.

    However, from a support perspective this is really beyond what we can do here in the forums. If you cannot determine your answer here or on your own, consider opening a support case with us. Visit this link to see the various support options that are available to better meet your needs:  http://support.microsoft.com/default.aspx?id=fh;en-us;offerprophone.

    We focus on various troubleshooting plan and solution on IIS web platform and distributed applications. Please contact us at:

    http://blogs.msdn.com/b/asiatech/