GSA / data.gov

Main repository for the data.gov service
https://data.gov
Other
619 stars 98 forks source link

Specifying application/json for CKAN API fails on Inventory #1177

Closed adborden closed 4 days ago

adborden commented 4 years ago

This looks like a bug in CKAN. When application/json is specified, it returns 400 and the message:

Bad Action API request data: Invalid request. Please use POST method for your request

Specifying application/x-www-form-urlencoded seems to keep it happy, even though it's parsing json.

I've only seen this with POST methods.

How to reproduce

  1. Create a data file for package_create, package_create.json:
    {"name": "neh-peer-review-evaluators-1970-present", "notes": "test", "tag_string": ["test"], "title": "NEH peer-review evaluators, 1970-present", "owner_org": "national-endowment-for-the-humanities"}
  2. curl -v -X POST -H 'Cookie: auth_tkt=1' -H "x-ckan-api-key: $CKAN_API_KEY" -H 'Content-Type: application/json; charset=utf-8' --data @package_create.json https://inventory-datagov.dev-ocsit.bsp.gsa.gov/api/action/package_create

Expected behavior

200

Actual behavior

400

Bad Action API request data: Invalid request. Please use POST method for your request

adborden commented 4 years ago

As a workaround, you should url encode your json data and specify Content-Type: application/x-www-form-urlencoded

import json
import urllib.parse
import requests

encoded_data = urllib.parse.quote(json.dumps(data_dict))
response = requests.post(url, data=encoded_data, headers={'Content-Type': 'application/x-www-form-urlencoded'})
adborden commented 4 years ago

Found this commit which also works around the issue for datapusher https://github.com/GSA/datagov-deploy/commit/1c3be95f88afc52a102f6d92df1e20f4678b425b

jbrown-xentity commented 4 days ago

Not a bug we will fix. Closing.