New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"Cannot unmarshal" error when creating a repo via the API #2535

Closed
simonszu opened this Issue Feb 1, 2016 · 6 comments

Comments

2 participants
@simonszu

simonszu commented Feb 1, 2016

Hi,

for an undergoing ansible setup, i want to create some repos via the API. So i made a POST-request against the API with the following json content to test the creation of repos via the API:
"{\"name\": \"test1\", \"private\": true, \"description\": \"etc files of calad server\"}".

Unfortunately i get an HTTP error 400 with the following content:
{\"classification\":\"DeserializationError\",\"message\":\"json: cannot unmarshal string into Go value of type gogs.CreateRepoOption\"}, {\"fieldNames\":[\"Name\"],\"classification\":\"RequiredError\",\"message\":\"Required\"}

So i tried the request with name and Name, but the error stays the same. I dug somewhere around gogs's code to check where the issue could occur, and why there is the API mocking about Name written in uppercase, but i could not find any hints.

I know that the API is still in beta, but all other API requests i have made (speak: Adding an SSH key to a user account) worked perfectly fine, so i'm just asking for help with this API call now.

@Unknwon

This comment has been minimized.

Show comment
Hide comment
@Unknwon

Unknwon Feb 1, 2016

Member

Hi, which version of Gogs are you using?

I just tried with following simple example:

{
    "name": "repo_create_by_api"
}

And works...

Maybe try one with https://try.gogs.io?

Member

Unknwon commented Feb 1, 2016

Hi, which version of Gogs are you using?

I just tried with following simple example:

{
    "name": "repo_create_by_api"
}

And works...

Maybe try one with https://try.gogs.io?

@Unknwon Unknwon added this to the 0.9.0 milestone Feb 1, 2016

@simonszu

This comment has been minimized.

Show comment
Hide comment
@simonszu

simonszu Feb 2, 2016

Hmm...this is funny. I have updated gogs to version 0.8.25.0129 and now it works via curl and the command line. But i still have problems with accessing the API via ansible.

Just for the records: This is the task executed by ansible:

  uri:
    url: https://git.simonszu.de/api/v1/user/repos
    method: POST
    status_code: 201,422
    user: simonszu
    password: XXXXXXXX
    force_basic_auth: yes
    body: "{\"title\": \"api-test\", \"description\": \"testing via api\", \"private\": false}"
    body_format: json

And this is the result:

fatal: [calad]: FAILED! => {"changed": false, "connection": "keep-alive", "content": "[{\"classification\":\"DeserializationError\",\"message\":\"json: cannot unmarshal string into Go value of type gogs.CreateRepoOption\"},{\"fieldNames\":[\"Name\"],\"classification\":\"RequiredError\",\"message\":\"Required\"}]", "content_length": "207", "content_type": "application/json; charset=utf-8", "date": "Tue, 02 Feb 2016 08:46:07 GMT", "failed": true, "invocation": {"module_args": {"backup": null, "body": "{\"title\": \"api-test\", \"description\": \"testing via api\", \"private\": false}", "body_format": "json", "content": null, "creates": null, "delimiter": null, "dest": null, "directory_mode": null, "follow": false, "follow_redirects": "safe", "force": null, "force_basic_auth": true, "group": null, "method": "POST", "mode": null, "owner": null, "password": "XXXXXXXX", "regexp": null, "remote_src": null, "removes": null, "return_content": false, "selevel": null, "serole": null, "setype": null, "seuser": null, "src": null, "status_code": ["201", "422"], "timeout": 30, "url": "https://git.simonszu.de/api/v1/user/repos", "user": "simonszu", "validate_certs": true}, "module_name": "uri"}, "json": [{"classification": "DeserializationError", "message": "json: cannot unmarshal string into Go value of type gogs.CreateRepoOption"}, {"classification": "RequiredError", "fieldNames": ["Name"], "message": "Required"}], "msg": "Status code was not [201, 422]", "redirected": false, "server": "nginx/1.9.10", "set_cookie": "lang=en-US; Path=/; Max-Age=2147483647, i_like_gogits=ccddd7047a51b366; Path=/; HttpOnly, _csrf=n2SG2URTg1w3Ru775TOyKvh6-gM6MTQ1NDQwMjc2NzgxODE5ODUxMg%3D%3D; Path=/", "status": 400}

What is funny is the following: In the same ansible playbook i make sure that the user's public key is already in gogs's database. The task is exactly the same, the only thing that changes is the URL and the content of the body. However, the inserting of the SSH key succeeds, while the creation of the repository does not.

I am quite sure that this isn't ansible's fault, and also not gogs's fault. But it has to be someone's fault ;)

What's irritating me is the error: {"classification": "RequiredError", "fieldNames": ["Name"], "message": "Required"}, though.


UPDATE: I have validated the JSON i am sending to the API with http://jsonlint.com/. It is a valid JSON.


Even more Update: I was suspicious about your test-json, which contained only the name, and no description or privacy-setting like the example in the API wiki. So i created a JSON with only the name, which, to my surprise, worked. So i added the description and the private-fields subsequently, and noticed that the request fails again after adding the private field. Will do further investigation.

simonszu commented Feb 2, 2016

Hmm...this is funny. I have updated gogs to version 0.8.25.0129 and now it works via curl and the command line. But i still have problems with accessing the API via ansible.

Just for the records: This is the task executed by ansible:

  uri:
    url: https://git.simonszu.de/api/v1/user/repos
    method: POST
    status_code: 201,422
    user: simonszu
    password: XXXXXXXX
    force_basic_auth: yes
    body: "{\"title\": \"api-test\", \"description\": \"testing via api\", \"private\": false}"
    body_format: json

And this is the result:

fatal: [calad]: FAILED! => {"changed": false, "connection": "keep-alive", "content": "[{\"classification\":\"DeserializationError\",\"message\":\"json: cannot unmarshal string into Go value of type gogs.CreateRepoOption\"},{\"fieldNames\":[\"Name\"],\"classification\":\"RequiredError\",\"message\":\"Required\"}]", "content_length": "207", "content_type": "application/json; charset=utf-8", "date": "Tue, 02 Feb 2016 08:46:07 GMT", "failed": true, "invocation": {"module_args": {"backup": null, "body": "{\"title\": \"api-test\", \"description\": \"testing via api\", \"private\": false}", "body_format": "json", "content": null, "creates": null, "delimiter": null, "dest": null, "directory_mode": null, "follow": false, "follow_redirects": "safe", "force": null, "force_basic_auth": true, "group": null, "method": "POST", "mode": null, "owner": null, "password": "XXXXXXXX", "regexp": null, "remote_src": null, "removes": null, "return_content": false, "selevel": null, "serole": null, "setype": null, "seuser": null, "src": null, "status_code": ["201", "422"], "timeout": 30, "url": "https://git.simonszu.de/api/v1/user/repos", "user": "simonszu", "validate_certs": true}, "module_name": "uri"}, "json": [{"classification": "DeserializationError", "message": "json: cannot unmarshal string into Go value of type gogs.CreateRepoOption"}, {"classification": "RequiredError", "fieldNames": ["Name"], "message": "Required"}], "msg": "Status code was not [201, 422]", "redirected": false, "server": "nginx/1.9.10", "set_cookie": "lang=en-US; Path=/; Max-Age=2147483647, i_like_gogits=ccddd7047a51b366; Path=/; HttpOnly, _csrf=n2SG2URTg1w3Ru775TOyKvh6-gM6MTQ1NDQwMjc2NzgxODE5ODUxMg%3D%3D; Path=/", "status": 400}

What is funny is the following: In the same ansible playbook i make sure that the user's public key is already in gogs's database. The task is exactly the same, the only thing that changes is the URL and the content of the body. However, the inserting of the SSH key succeeds, while the creation of the repository does not.

I am quite sure that this isn't ansible's fault, and also not gogs's fault. But it has to be someone's fault ;)

What's irritating me is the error: {"classification": "RequiredError", "fieldNames": ["Name"], "message": "Required"}, though.


UPDATE: I have validated the JSON i am sending to the API with http://jsonlint.com/. It is a valid JSON.


Even more Update: I was suspicious about your test-json, which contained only the name, and no description or privacy-setting like the example in the API wiki. So i created a JSON with only the name, which, to my surprise, worked. So i added the description and the private-fields subsequently, and noticed that the request fails again after adding the private field. Will do further investigation.

@simonszu

This comment has been minimized.

Show comment
Hide comment
@simonszu

simonszu Feb 2, 2016

Well - i think i fixed it. Somehow i had a double-json-casting in my ansible task. In the task was a normal json created, which then was encapsulated in json again. This was no problem with the API-access of /users/keys, since a jsonified string becomes a string again.
The problem was the boolean in the /users/repo API access. A jsonified boolean becomes a string, and this is clearly not what the API was expecting.

simonszu commented Feb 2, 2016

Well - i think i fixed it. Somehow i had a double-json-casting in my ansible task. In the task was a normal json created, which then was encapsulated in json again. This was no problem with the API-access of /users/keys, since a jsonified string becomes a string again.
The problem was the boolean in the /users/repo API access. A jsonified boolean becomes a string, and this is clearly not what the API was expecting.

@simonszu simonszu closed this Feb 2, 2016

@Unknwon

This comment has been minimized.

Show comment
Hide comment
@Unknwon

Unknwon Feb 2, 2016

Member

:trollface: Great!

Member

Unknwon commented Feb 2, 2016

:trollface: Great!

@simonszu

This comment has been minimized.

Show comment
Hide comment
@simonszu

simonszu Feb 2, 2016

Why do you always attach a trollface to my issues? :D
It isn't so that i want to just troll you, although it could seem so. I just spent two days debugging this stuff. Glad i figured it out at last ;)

simonszu commented Feb 2, 2016

Why do you always attach a trollface to my issues? :D
It isn't so that i want to just troll you, although it could seem so. I just spent two days debugging this stuff. Glad i figured it out at last ;)

@Unknwon

This comment has been minimized.

Show comment
Hide comment
@Unknwon

Unknwon Feb 2, 2016

Member

No, it means shit happens and I understand you :trollface:

Member

Unknwon commented Feb 2, 2016

No, it means shit happens and I understand you :trollface:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment