Skip to content
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 force Content-Type header after form fields apply #488

Closed
ivann7 opened this issue Aug 6, 2018 · 8 comments
Closed

Cannot force Content-Type header after form fields apply #488

ivann7 opened this issue Aug 6, 2018 · 8 comments
Assignees

Comments

@ivann7
Copy link

ivann7 commented Aug 6, 2018

Example

Feature: Test
  Background:
    * configure ssl = true
    Given url 'https://httpbin.org'
  Scenario: Bug 1
    Given path '/post'
    * def forms_1 =
    """
    {
      test: "123"
    }
    """
    And form fields forms_1
    #Not working
    * configure headers = { 'Content-Type': 'application/json' }
    #Not working too, Content-Type always 'application/x-www-form-urlencoded'
    And header Content-Type = 'application/json'
    When method POST
    Then status 200
@ptrthomas ptrthomas self-assigned this Aug 7, 2018
@ptrthomas ptrthomas added the bug label Aug 7, 2018
@ptrthomas ptrthomas added this to the 0.9.0 milestone Aug 7, 2018
@ptrthomas ptrthomas added the fixed label Aug 7, 2018
@ptrthomas
Copy link
Member

@inik7 great catch ! thanks for the info on how to replicate. fixed in dev

as a workaround, you can prepare the request body manually:

    Given url 'https://httpbin.org'
    And path '/post'
    And request 'test=123'
    And header Content-Type = 'application/json'
    When method post
    Then status 200

@ivann7
Copy link
Author

ivann7 commented Aug 7, 2018

Thanks a lot for fixing and advice for workaround!
But really pain starts when I want custom or dynamic param in fields.

Feature: Test
  Background:
    * configure ssl = true
    Given url 'https://httpbin.org'
  Scenario: Workaround
    Given path '/post'
    * def dynamic_param = 'abcdef'
    * def forms_1 =
    """
    {
      test: "#(dynamic_param)"
    }
    """
#    Not work, because of content type
#    And form fields forms_1

#    Not work because request do not support params
#    And request '#(dynamic_param)'

#    Work only with custom method, that returns raw string from given map
#    Example of method in Groovy:
    
#    static String mapToFormFieldsRawString(Map<String, String> m) {
#      List<NameValuePair> params = m.collect { k, v -> new BasicNameValuePair(k, v) }
#      URLEncodedUtils.format(params, UTF_8)
#    }

    And request mapToFormFieldsRawString(dynamic_param)
    And header Content-Type = 'application/json'
    When method POST
    Then status 200

@ptrthomas
Copy link
Member

hello there. I think you are doing it wrong, and you first confused me with form field. have you even read the docs ? and examples ? for e.g. this: https://github.com/intuit/karate/blob/master/karate-demo/src/test/java/demo/cats/kittens.feature

if all you are trying to do is submit a dynamic JSON, please just return a Map from your Java method and use it directly. I also suspect that you are using Java un-necessarily - which is a common mistake by folks new to Karate who don't understand it.

If you need any further assistance on this issue, first read the docs and then follow the instructions here: https://github.com/intuit/karate/wiki/How-to-Submit-an-Issue

@ivann7
Copy link
Author

ivann7 commented Aug 7, 2018

The idea is to send the body 'application/ x-www-form-urlencoded' with the header 'application/json'
Example:

Body: test1=abc&test2=def
Header: Content-Type = 'application/json'

@ptrthomas
Copy link
Member

is this a negative test case ? because this is not correct HTTP behavior. no body ever needs to do this.

so please do not complain about "pain starts" !

if you need any thing else, make sure you follow the instructions.

@ivann7
Copy link
Author

ivann7 commented Aug 7, 2018

Ok) Yes, negative tests. Force header solve the problem :)

@ptrthomas
Copy link
Member

ptrthomas commented Aug 7, 2018

I guessed it. just hard-code a string request and please don't try to implement dynamic parameters for this negative test case ! anyway in the next version it will be possible to over-ride the content-type for form field but I now think it was not necessary :|

@ptrthomas
Copy link
Member

0.9.0 released

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

No branches or pull requests

2 participants