JSON RPC: I always get "JSON-RPC requests must have the Content-type: application/json". Why?

Davide Cavestro
Contributor
November 2, 2011

Hi all,

I'm trying to access my company's jira test instance through the JSON RPC services (as per https://developer.atlassian.com/display/JIRADEV/JIRA+JSON-RPC+Overview ) but I always get a negative response like

[id:null, error:[message:JSON-RPC requests must have the Content-type: application/json, data:null, code:-32600], jsonrpc:2.0]

even if I always send a "Content-Type" request header set to "application/json".

My JIRA instance has the JSON RPC plugin enabled. Moreover I get the same error trying to access https://jira.atlassian.com (although I don't know if that instance has json rpc enabled)

Here you are a groovy script that automates the login through JSON RPC on https://jira.atlassian.com: what's wrong with it?

@Grab(group='org.codehaus.groovy.modules.http-builder', module='http-builder', version='0.5.0-RC2' )
import groovyx.net.http.*
import static groovyx.net.http.ContentType.*
import static groovyx.net.http.Method.*

def http = new HTTPBuilder( 'https://jira.atlassian.com' )

// perform a GET request, expecting JSON response data
http.request (POST, JSON){
  uri.path = '/rpc/json-rpc/jirasoapservice-v2'
  uri.query = [
	  "jsonrpc" : "2.0",
	  "method" : "login",
	  "params" : ["user name","user password"],
	  "id" : 12345
  ]
  
  
  headers.'User-Agent' = 'Mozilla/5.0 Ubuntu/8.10 Firefox/3.0.4'
  headers.'Content-Type' = 'application/json; charset=UTF-8'
  
  // response handler for a success response code:
  response.success = { resp, json ->
    println resp.statusLine
    println "  json : ${json}"
  }

  // handler for any failure status code:
  response.failure = { resp ->
    println "Unexpected error: ${resp.statusLine.statusCode} : ${resp.statusLine.reasonPhrase}"
  }
}

Cheers

Davide

5 answers

1 vote
Davide Cavestro
Contributor
November 6, 2011

The server expects exactly the 'application/json' value as content-type (not 'application/json; charset=UTF-8').
It's an issue that has been solved on the main trunk for version 1.0.3 (I'm using version 1.0.2 of jsonrpc plugin).

Jared Farrish
Contributor
March 23, 2012

Hello Davide,

I'm having the same problem with JIRA 4.4.3 and in the included JSON-RPC plugin (ver. 1.0.2), only I'm using Javascript to submit the JSON-RPC request. My problem at this point is figuring out how to get this plugin updated to the 1.0.3 version for JIRA (not Confluence). Since this was "ported" from the Confluence plugin, it doesn't appear that disabling the System-installed version and installing the version from the Plugin Exchange page is working. Do you know how to get this plugin to the new version in JIRA 4.4.3?

Thanks!

Jared

JamieA
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
March 23, 2012

You have to check it out from github, build it, install it. I had to do this too, it worked fine for me.

Jared Farrish
Contributor
March 23, 2012

Thanks Jamie.

0 votes
Davide Cavestro
Contributor
November 6, 2011

Debugging has been very handy... follows a version of the groovy script that works properly (in the previous version I used application/json; charset... instead of application/json and uri.query instead of body). Although I still haven't found a way to use the light protocol.

@Grab(group='org.codehaus.groovy.modules.http-builder', module='http-builder', version='0.5.0-RC2' )
import groovyx.net.http.*
import static groovyx.net.http.ContentType.*
import static groovyx.net.http.Method.*

def http = new HTTPBuilder( 'http://jira.my.lan:8080/' )

// perform a GET request, expecting JSON response data
http.request (POST, JSON){req->
  uri.path = '/rpc/json-rpc/jirasoapservice-v2'
  body = [
	  "jsonrpc" : "2.0",
	  "method" : "login",
	  "params" : ["myuser","mypass"],
	  "id" : 12345
  ]
	  
//  uri.path = '/rpc/json-rpc/jirasoapservice-v2/login'
//  body = [
//	  "myuser","mypass"
//  ]

  headers.'User-Agent' = 'Mozilla/5.0 Ubuntu/8.10 Firefox/3.0.4'
  headers.'Content-Type' = 'application/json'
  
  // response handler for a success response code:
  response.success = { resp, json ->
    println resp.statusLine

	println "  json : ${json}"
  }

  // handler for any failure status code:
  response.failure = { resp ->
    println "Unexpected error: ${resp.statusLine.statusCode} : ${resp.statusLine.reasonPhrase}"
  }
}

0 votes
JamieA
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
November 3, 2011

The username/password on line 15 looks odd.

It's this that is throwing: https://bitbucket.org/cmiller_atlassian/confluence-json-rpc-plugin/annotate/31356045ef00/atlassian-voorhees/src/main/java/com/atlassian/voorhees/JsonRpcHandler.java#line-62

If it was me I'd probably install jira locally and attach the debugger, and see what is going on there. Can't see any obvious problem with that code. Maybe charset?

BTW fiddler does not need to run on the same machine, I guess you have access to a windows box.

0 votes
Davide Cavestro
Contributor
November 3, 2011

The user agent header was only a test... it comes from an example snippet I took from a groovy tutorial.

I cannot use Fiddler as I'm on a linux host. This is the conversation recorded through LiveHttpHeaders while trying to forge the login request using REST Client plugin on Firefox

http://jira.my.lan:8080/rpc/json-rpc/jirasoapservice-v2/login
 
POST /rpc/json-rpc/jirasoapservice-v2/login HTTP/1.1
Host: jira.my.lan:8080
User-Agent: Mozilla/5.0 (X11; Linux i686 on x86_64; rv:6.0.2) Gecko/20100101 Firefox/6.0.2
Accept: application/json
Accept-Encoding: gzip, deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Connection: keep-alive
Content-Type: application/json; charset=UTF-8
Content-Length: 19
Cookie: seraph.rememberme.cookie=10202%3Ab5bba9762e71b1c42603e437741badf344c9ac81; jira.toggleblocks.cong.cookie=; atlassian.xsrf.token=B5QP-6EHS-TVLW-OY80|6e535d83233f682fe9d989e08032180c189f2abe|lin; JSESSIONID=89AECCE8F54D8922D4DF1F20B2D7AAB1
Pragma: no-cache
Cache-Control: no-cache
["myname","mypass"]


HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
X-AREQUESTID: 950x2031x1
X-ASESSIONID: 50h2jj
X-Seraph-LoginReason: OK
Content-Type: application/json;charset=UTF-8
Content-Length: 138
Date: Fri, 04 Nov 2011 14:50:58 GMT 

The output invariably remains
{"id":null,"error":{"message":"JSON-RPC requests must have the Content-type: application/json","data":null,"code":-32600},"jsonrpc":"2.0"}

Any idea?

Cheers

Davide

0 votes
JamieA
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
November 3, 2011

Out of interest why are you masquerading as firefox? Also httpbuilder should add the content-type header automatically, maybe the server is getting confused by having two?

I would recommend proxying through fiddler to see exactly what you are sending, although it can be a pain with SSL. You have to fiddle around with the certificate to get fiddler to see it.

Suggest an answer

Log in or Sign up to answer